You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH numdiff vectorized for scalar parameter functions #1783
unit tests in test_numdiff almost only check correctness of numbers. There is one type test to allow for list.
AFAICS, all cases are for one function w.r.t. one vector parameter.
I don't see from looking at the test code whether there are vector returns of the objective function,
Making the change is not backwards compatible, but the case is not mentioned in the docs and it should be a bugfix.
It is implied in the docstring that we have only one params vector.
The only user of the vectorized params case might be our genmod Link classes.
Behavior for vector return of the objective function is well defined for 1-d and 2-d
from the Notes section of approx_fprime
If f returns a 1d array, it returns a Jacobian. If a 2d array is returned
by f (e.g., with a value for each observation), it returns a 3d array
with the Jacobian of each observation with shape xk x nobs x xk. I.e.,
the Jacobian of the first observation would be [:, 0, :]
The new case is that the function has only a single parameter, i.e. R ->R or R -> R^k, but we can evaluate it at many different params. The vectorized params needs to be a column array.
2-d return by the function seems to work also, but params needs to have extra dimensions to broadcast directly. Broadcasting needs to be handled in the user function, so that can be messy.
2-d function return looks too tricky to support and explain in general
e.g. vectorized over params p for function that returns results corresponding to shape of a. func broadcasts between params p and "data" a.
I haven't tried approx_fprime_cs, but it looks like the same change to .shape should work
Note
Using .shape attribute requires numpy and is more restrictive than len.
So, there might still be problems with this fix for non-numpy array parameters. I didn't check.
correction to backwards compatibility:
Our cases in links cdflink using np.diag(approx_fprime.. do not break.
If parameter array is 1-dim, then there is no change because len(x) == x.shape(-1) for 1-dim.
Consquently, we do not have failing unit test cases to find the refactoring cases.
changing only len(x) is not enough.
get_epsilon uses elementwise maximum: h = EPS**(1. / s) * np.maximum(np.abs(x), 0.1)
Trying to fix it so that it handles also the new case gets messy. Instead I special case the code for the vectorized scalar parameter case if x.ndim == 2 and n == 1:
This has now epsilon varying by row depending on the parameter.
The simple fix to approx_fprime, does not adjust parameter specific epsilon correctly. It takes now just the epsilon for the first case of the vectorized parameter.
My new t(1)/cauchy test in #7287 fails and has less precision tol around 1e-5 instead of 1e-8 using vectorized approx_fprime compared the the diag version.
I didn't refactor approx_fprime further. I created new function _approx_fprime_scalar that only handles scalar parameter case, but in vectorized form. This treats also 1-dim params as vectorized scalar param, not as a single vector parameter as does approx_fprime.
More generalizations are left for the future when or if we need them.
t(1)/cauchy cdflink test passes again with epsilon scaled by individual parameters.
approx_fprime_cs cannot be used for cdflinks because much of scipy.stats.distributions does not support complex numbers.
Activity
josef-pkt commentedon Jun 24, 2014
josef-pkt commentedon Jan 6, 2021
bump
I'm running into the same testing new transformation functions
[-]ENH numdiff vectorized for scalar function[/-][+]ENH numdiff vectorized for scalar parameter functions[/+]josef-pkt commentedon Jan 6, 2021
maybe trivial
I copied the approx_fprime code, and needed to change n=len(x) to n = x.shape[-1].
This works for the example
unit tests in test_numdiff almost only check correctness of numbers. There is one type test to allow for list.
AFAICS, all cases are for one function w.r.t. one vector parameter.
I don't see from looking at the test code whether there are vector returns of the objective function,
josef-pkt commentedon Jan 6, 2021
Making the change is not backwards compatible, but the case is not mentioned in the docs and it should be a bugfix.
It is implied in the docstring that we have only one params vector.
The only user of the vectorized params case might be our genmod Link classes.
Behavior for vector return of the objective function is well defined for 1-d and 2-d
from the Notes section of approx_fprime
The new case is that the function has only a single parameter, i.e. R ->R or R -> R^k, but we can evaluate it at many different params. The vectorized params needs to be a column array.
2-d return by the function seems to work also, but params needs to have extra dimensions to broadcast directly. Broadcasting needs to be handled in the user function, so that can be messy.
2-d function return looks too tricky to support and explain in general
e.g. vectorized over params p for function that returns results corresponding to shape of a.
funcbroadcasts between paramspand "data"a.caveat is that results are transposed
scalar return
1-dim params keeps current behavior with the diag result
with 2-dim column params, we get new behavior derivative at each point of params
1-dim return by function
vectorized params needs to broadcast within the function, and needs p.shape[-1] == 1 in p-vectorized fprime usage
josef-pkt commentedon Jan 6, 2021
timing huge speedup, but more important, the old diag version uses (len(p), len(p)) sized arrays, len(p) = nobs in our applications
loop is also slow because the example function does little work and loop overhead dominates
It looks like params needs to be an atleast_1d array in approx_fprime
josef-pkt commentedon Jan 6, 2021
I haven't tried
approx_fprime_cs, but it looks like the same change to.shapeshould workNote
Using
.shapeattribute requires numpy and is more restrictive thanlen.So, there might still be problems with this fix for non-numpy array parameters. I didn't check.
(I'm back to other parts)
josef-pkt commentedon Sep 1, 2021
correction to backwards compatibility:
Our cases in links cdflink using
np.diag(approx_fprime..do not break.If parameter array is 1-dim, then there is no change because len(x) == x.shape(-1) for 1-dim.
Consquently, we do not have failing unit test cases to find the refactoring cases.
change will be included in cdflink PR #7287
josef-pkt commentedon Sep 1, 2021
approx_fprime_cs
changing only len(x) is not enough.
get_epsilon uses elementwise
maximum:h = EPS**(1. / s) * np.maximum(np.abs(x), 0.1)Trying to fix it so that it handles also the new case gets messy. Instead I special case the code for the vectorized scalar parameter case
if x.ndim == 2 and n == 1:This has now epsilon varying by row depending on the parameter.
The simple fix to approx_fprime, does not adjust parameter specific epsilon correctly. It takes now just the epsilon for the first case of the vectorized parameter.
My new t(1)/cauchy test in #7287 fails and has less precision tol around 1e-5 instead of 1e-8 using vectorized approx_fprime compared the the diag version.
josef-pkt commentedon Sep 1, 2021
I didn't refactor approx_fprime further. I created new function
_approx_fprime_scalarthat only handles scalar parameter case, but in vectorized form. This treats also 1-dim params as vectorized scalar param, not as a single vector parameter as does approx_fprime.More generalizations are left for the future when or if we need them.
t(1)/cauchy cdflink test passes again with epsilon scaled by individual parameters.
approx_fprime_cs cannot be used for cdflinks because much of scipy.stats.distributions does not support complex numbers.
josef-pkt commentedon Sep 3, 2021
closing,
scalar param case has been added in #7287
general 2-d params case is open, #7680