New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH numdiff vectorized for scalar parameter functions #1783
Comments
|
bump |
maybe trivial I copied the approx_fprime code, and needed to change n=len(x) to n = x.shape[-1].
unit tests in test_numdiff almost only check correctness of numbers. There is one type test to allow for list. |
Making the change is not backwards compatible, but the case is not mentioned in the docs and it should be a bugfix. 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. e.g. vectorized over params p for function that returns results corresponding to shape of 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
|
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
|
I haven't tried Note (I'm back to other parts) |
correction to backwards compatibility: Consquently, we do not have failing unit test cases to find the refactoring cases. change will be included in cdflink PR #7287 |
approx_fprime_cs changing only len(x) is not enough. 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 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 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. |
I'm using a vectorized derivative for a scalar parameter and get a diagonal matrix back instead of the 1d vectorized version.
using
diag(deriv)
worksWe need a way to disambiguate how 1d parameters are treated.
no standalone example, this was debugging in PR #1781 derivative of var and link functions
josef-pkt@6433db8
The text was updated successfully, but these errors were encountered: