--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/2e3173ca-f6ca-40e6-9bce-1a3ce479774en%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/uP4bSIwozW4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/7585645D-57A9-4EA7-9D48-3F7972302A07%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/7585645D-57A9-4EA7-9D48-3F7972302A07%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESboREbLxabZosGdqD3qHbQAMBQAnWTUb-xHFfDGUWMM5mQ%40mail.gmail.com.
Good day!This question is being asked in this group with a certain periodicity (thread from 2020). Yet, lmfit authors continue to dismiss the existence of correlated noise and generalized least squares method that handles it.
Um, "continue to dismiss"? No. Neither Renee nor I dismissed the question. In fact, we both responded. That, in and of itself, demonstrates interest and is a time commitment. Some random person on the internet asked about fitting data and we responded. We did not say "no we do not want to do that", just "we do not know how to do that".
For image data, neighboring pixels may very well be correlated, so, weighting accordingly is a fine idea. Is it general? Nope. Sometimes "correlation" might mean neighboring data bins, and sometimes it might mean harmonics or other ways of adding frequencies.
For one of the data types I work with (and I believe for one of the kinds of data Renee works on too), using Fourier or similar transforms of the data is really common: measure the data in time, wavenumber domain but transform to frequency or distance domain to do the fit. In that case, all the data is correlated and things like sampling rates and Nyquist frequencies need to be considered.
Is there a generalized method to do that that is independent of the model or form of the data? I am not aware of one.
Your solution literally uses `minimize()`, and is probably a totally fine approach. But `minimize()` does not **by itself** take correlations between data into account.
--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESbq7XMRWH2PKHQXZOFcf0c074oWixgL-A8VF6OS%2B6s6Hqg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CANd6mErVOMf1MKT5J8VbdNEQMBBN3wcLt1khDpaSrvxX9EvoDg%40mail.gmail.com.
You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/uP4bSIwozW4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/3F836C2B-D90B-40A3-A7EE-CB3A81E9033B%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/3F836C2B-D90B-40A3-A7EE-CB3A81E9033B%40gmail.com.
You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/uP4bSIwozW4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESbpdHjmUwae1vDQwxWFboo5-V_99DsgpbXE%2BTPbKAREF7Q%40mail.gmail.com.
Yeah, thanks for that. I agree that it seems completely possible to add this to the Model class, but some details might need to be worked out. My recollection (probably dim) from 2 years was that there was not an elegant way to handle covariances that would lead to "negative effective uncertainties", which could happen mathematically, but have no clear meaning.
Like, I think the idea is to extend uncertainty `epsilon` to `sqrt(epsilon * [(1/covar) . epsilon])` (if I have that correct -- going from memory here). The Fortran programmer in me worries about that sqrt(). It is one thing to have that work in a carefully crafted example and quite another to support it for others. And we would want to extend "weight" to "1/epsilon". ;)
Maybe the solution is "well known" and we (or to be clear: I) just don't know it - that is certainly possible. For sure, one of the challenges for open-source scientific software is that agreeing on terminology can be challenging. Everyone involved in the development and support has to be able to explain it and work with the code. So, some random person on the internet pointing to a PDF or web page and saying "see it is obvious, you morons, you just don't care to solve the problem" actually does not understand the problem at all. We need solutions in Python that are robust and that we can support.
It is disappointing to see, especially from a scientist in a closely related field, but there seems no point in engaging with someone with that attitude.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESbpdHjmUwae1vDQwxWFboo5-V_99DsgpbXE%2BTPbKAREF7Q%40mail.gmail.com.
Tarek, all,Yeah, thanks for that. I agree that it seems completely possible to add this to the Model class, but some details might need to be worked out. My recollection (probably dim) from 2 years was that there was not an elegant way to handle covariances that would lead to "negative effective uncertainties", which could happen mathematically, but have no clear meaning.Diagonal elements of covariance matrix, square root of which gives us uncertainties (epsilon in lmfit notation) cannot be negative as you correctly point out! However, correlations between data points can be negative. Those will result in off-diagonal elements being negative. An important condition that covariance matrix must satisfy is that it has to be positive semi definite, which is not the same as not having any negative elements. So a covariance matrix where off-diagonal elements are negative can be a valid one!
Like, I think the idea is to extend uncertainty `epsilon` to `sqrt(epsilon * [(1/covar) . epsilon])` (if I have that correct -- going from memory here). The Fortran programmer in me worries about that sqrt(). It is one thing to have that work in a carefully crafted example and quite another to support it for others. And we would want to extend "weight" to "1/epsilon". ;)The 2020 thread provides lengthy explanations with working examples showing that this is a bad idea.
However, you have the right intuition that we should do something like square root of the covariance matrix. Square rooting matrices is not as straightforward as scalars though! The good news is that linear algebra does extend the concept of square roots into the matrix realm/ The square roots of matrices can be computed in many ways (wiki page on matrix square root is a good resource to learn about it) and are not unique except for very special cases. This ambiguity does not matter though In the context of optimization/fitting. In fact, Cholesky decomposition I used in my solution is one of the definitions of matrix square root. I used it for convenience since it requires only one line of code.
Maybe the solution is "well known" and we (or to be clear: I) just don't know it - that is certainly possible. For sure, one of the challenges for open-source scientific software is that agreeing on terminology can be challenging. Everyone involved in the development and support has to be able to explain it and work with the code. So, some random person on the internet pointing to a PDF or web page and saying "see it is obvious, you morons, you just don't care to solve the problem" actually does not understand the problem at all. We need solutions in Python that are robust and that we can support.In the entire script I enclosed only two lines of code take care of the correlations. If an implementation of two lines of code that use standard libraries (such as numpy) is not robust/simple then I don’t know what is.In fact, scipy’s curve_fit uses exactly the same approach with cholesky decomposition of covariance matrix:Their implementation is a bit different technically (they really take advantage of the Cholesky factor's triangular structure) and it most likely results in a better computational performance. However, the methodology is the same.
I started to look at this a bit more and plan to make a PR to add support the use of the covariance matrix to fit correlated data.I’ve added the example from Denis to show how to do this when using the Minimizer class directly. There is no change in the code needed here, just a demonstration of how to use the Cholesky factor to take the correlation of the data into account in the residual function.For the Model class, I *think* the easiest/cleanest way to implement this is to add another argument (for example, “cov_data” os something similar) to the .fit() method. Although in theory possible (i.e., by checking the dimensions of the array as scipy.optimize.curve_fit does), I’d rather not re-use “weights” for this purpose. Matt and others: do you agree on this; just checking before starting to make the actual changes.