Skip to content
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

Draft: Added computation of derivatives #5

Merged
merged 33 commits into from
Jun 23, 2022
Merged

Conversation

JCGoran
Copy link
Owner

@JCGoran JCGoran commented Jun 15, 2022

This PR adds some basic functionality to compute derivatives in fitk using finite differences (see for instance this Wiki article).
In principle, the interface is completely generic, and any code can be made to compute Fisher matrices (and higher order corrections) by just inheriting from FisherDerivative, and implementing the signal and covariance methods with appropriate return types, without having to touch any other aspect of either fitk, or the other code.

TODO:

  • verify that second order mixed derivatives with non-central coefficients work properly in a variety of situations
  • implement third order mixed derivatives and test them
  • figure out the most appropriate naming and calling convention; currently, there is a separate class (called D to mimic Mathematica's D) for specifying details about the parameter (its name/label, the point at which we wish to compute the derivative, the order of the derivative, the absolute step size); this is nice because defaults can be set in the class, but has the disadvantage that it's possible to forget to call it. The actual computation is defined in a dunder __call__, maybe calling it derivative would be more intuitive (and it'd be easier to display and explain it in the docs)
  • add a way to specify kind when calling fisher_tensor (for a concrete example, it's impossible to use central differences in cosmological codes for neutrino masses if we set the fiducial value to zero); currently it uses the default, which is center
  • speaking of fisher_tensor, figure out if that is the most appropriate name for the method (resolved for now: current name is fisher_matrix)
  • add the module to the online docs
  • figure out how to return coordinate metadata (maybe signal should return the tuple (coords, values)? Or a separate (data) class with properties coords and values). Note that the coordinates are not needed anywhere in the code, they would just be there as a convenience for the user, in case they want to construct the derivatives themselves, or if they want to easily plot the derivatives without re-computing the coordinates.
  • (nice to have) implement adaptive step size so the user doesn't have to guess whether the derivative computed has actually converged (and hasn't experienced underflow)
  • implement interfaces to various well-known codes:
    • COFFE
    • CLASS
    • CAMB
    • PyCosmo
  • add ability to specify a custom stencil when computing a derivative
  • (nice to have) make a metaclass that can be used to streamline the creation of interfaces to third-party software
  • (nice to have) implement computation of derivatives using the complex step approximation

Single-variable derivatives seem to work, and so do mixed, second order,
central, partial derivatives; however, when using forward or backward
stencils, they do not appear to be very accurate yet.
@JCGoran JCGoran added the enhancement New feature or request label Jun 15, 2022
@JCGoran JCGoran self-assigned this Jun 15, 2022
Now it uses the `D` class, which makes it possible to specify the kind
of derivative we wish to compute (center, forward, of backward).
@JCGoran
Copy link
Owner Author

JCGoran commented Jun 15, 2022

Codecov Report

Merging #5 (fc926f7) into master (f29871b) will increase coverage by 1.13%.
The diff coverage is 99.58%.

@@            Coverage Diff             @@
##           master       #5      +/-   ##
==========================================
+ Coverage   92.70%   93.83%   +1.13%     
==========================================
  Files           6        8       +2     
  Lines        1233     1459     +226     
==========================================
+ Hits         1143     1369     +226     
  Misses         90       90              
Impacted Files Coverage Δ
fitk/fisher_derivative.py 98.73% <98.73%> (ø)
fitk/__init__.py 100.00% <100.00%> (ø)
fitk/fisher_matrix.py 88.26% <100.00%> (ø)
fitk/fisher_operations.py 100.00% <100.00%> (ø)
fitk/fisher_plotter.py 85.06% <100.00%> (+0.06%) ⬆️
fitk/fisher_utils.py 90.65% <100.00%> (+0.08%) ⬆️
fitk/interfaces/misc_interfaces.py 100.00% <100.00%> (ø)
tests/test_fitk.py 99.82% <100.00%> (+0.24%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

The computation can now handle arbitrary order derivatives with
arbitrary accuracy, and is limited only by standard floating-point math
issues. The tests have been modified accordingly to reflect this change.
Since the numerical errors start to show up at orders much smaller than
10, it's best to limit the computation to that order, and raise an error
otherwise.
Now it raises `NotImplementedError` instead of returning
`NotImplemented`, since the former should be more clear to users if
attempting to compute the Fisher matrix without implementing it first.
I forgot to take the inverse of the covariance in the 1D case.
This is where all of the "official" interfaces to the derivatives should
be placed.
According to PEP 585, the use of `Tuple`, `Dict`, and
`collections.abc.*` will be deprecated in future versions. On the other
hand, owing to the `annotations` from the `__future__` module, it's
possible to use `dict`, `tuple`, etc. as type hints in Python 3.7, and
this commit implements this change.
Forgot to change a handful of instances of `Tuple` and `abc.Mapping` in
the last commit
Forgot to put `from __future__ import annotations` in the tests
The `Collection`, `Mapping`, etc. from the `typing` module has also been
deprecated, and we now use their `collections.abc` versions instead.
The parameter-dependent covariance was not handled properly in all
cases, now it should work correctly as long as the return values of
`signal` and `covariance` can be matrix-multiplied.
Also enabled passing of arbitrary `kwargs` when calling `fisher_tensor`,
to be consistent with the signature of `__call__`.
It makes sense for now since higher order corrections have not been
implemented yet.
Also added tests for it
The implementation is using the Cython interface of COFFE.
Note that it has been purposely excluded from the coverage since the
code may be difficult to install on the remote.
@JCGoran JCGoran merged commit bd68888 into master Jun 23, 2022
@JCGoran JCGoran mentioned this pull request Jun 23, 2022
11 tasks
@JCGoran JCGoran deleted the feature/derivatives branch August 20, 2022 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant