-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add fitting functionality using Ceres-Solver #189
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting -- thanks for bringing this in and addressing such an important case.
I can't promise these are all my comments, yet, but it's a start...
int get_dtype(const bp::object &); | ||
|
||
template <typename T> | ||
T _calculate_median(const T*, const int); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newline
// Model independent Negative Log Likelihood for generalized | ||
// unconstrained minimization | ||
template <typename Model> | ||
struct NegLogLikelihood |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's important to point out, in the comment and/or name of the class, that this is what you should use for fitting power spectra. One should use this only on data whose residuals are expected to be Chi2(1)-distributed. Is it accurate to call it PowerSpectrumCostFunction?
} | ||
|
||
template <typename T> | ||
bool _invert_matrix(const T* matrix, T* inverse, const int n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this code copyright-free?
" p: parameter array (float32/64) with dimensions (ndets, nparams). This is modified in place.\n" | ||
" c: uncertaintiy array (float32/64) with dimensions (ndets, nparams). This is modified in place.\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For p and c -- do the values going in matter? Or can it just be zeros/empty?
"method that minimizes a log likelihood. OMP is used to parallelize across dets (rows)." | ||
"\n" | ||
"Args:\n" | ||
" f: frequency array (float32/64) with dimensions (nsamps,).\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there requirements on f? Must it be positive definite; must it be strictly increasing?
// index for f > lower fwhite | ||
for (int i = 0; i < nsamps; ++i) { | ||
if (f[i] > fwhite_lower) { | ||
fwhite_i.push_back(i); | ||
break; | ||
} | ||
} | ||
|
||
// index for f < upper fwhite | ||
for (int i = nsamps - 1; i >= 0; --i) { | ||
if (f[i] < fwhite_upper) { | ||
fwhite_i.push_back(i); | ||
break; | ||
} | ||
} | ||
|
||
int fwhite_size = fwhite_i[1] - fwhite_i[0] + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks segfaulty. Make it robust against fwhite_lower / fwhite_upper pathologically dodging your tests. A good way is like
int fwhite_lo = 0;
while (fwhite_lo < nsamps && f[fwhite_lo] < fwhite_lower)
fwhite_lo++;
fwhite_hi = fwhite_lo;
whilte (...
This branch is an attempt at adding fitting functionality to
so3g
using theCeres-Solver
library to support both non-linear least squares and general optimization problems. It is used to duplicate the existing noise-fitting function insotodlib
. The idea of this function is to:The cost function classes and fitting functions have been written in a model-independent manner, so they should be easy to adapt for other data and model types.
Ceres
includes options for bounds/constraints and supports auto differentiation so derivatives and gradients don't need be calculated manually. The general minimization routine forCeres
doesn't seem to have uncertainty calculations (its least-squares routines do) so I have manually calculated the inverted Hessian matrix to get the parameter uncertainties.I put most of the code into new
fitting_ops.cxx
andfitting_ops.h
files instead of putting it all inarray_ops.cxx
. I added anarray_ops.h
file to hold shared functions declarations.I've added a cmake directory with cmake files for
Ceres,
Eigen
,Gflag
, andGlog
with the last 3 being dependencies ofCeres-Solver
. We could move these intospt3g
at some point, but this also seems to work in my tests.Eigen
is a nice optimized vector, matrix, and linear algebra library that might be useful on its own, but it is not required to useEigen
when working withCeres
.Building the Docker image for tests will take a few minutes longer now as I found that Ceres needed to be built from source as the necessary version doesn't appear to be available through
apt-get libceres-dev
.