Repository navigation
Iss2928 fix stats.moment_helpers inplace modification - #3362
zverianskii wants to merge 8 commits into
Conversation
|
@zveryansky Thanks |
josef-pkt
left a comment
There was a problem hiding this comment.
(I will look some more a bit later)
I think we can use the test cases and adjust the code until they pass. My guess is that a copy at the right place should be enough.
| all_f = ['cum2mc', 'cum2mc', 'mc2cum', 'mc2mnc', | ||
| 'mc2mvsk', 'mnc2cum', 'mnc2mc', 'mnc2mc', | ||
| 'mvsk2mc', 'mvsk2mnc'] | ||
| assert np.all([isinstance(getattr(moment_helpers,f)([1.0, 1, 0, 3]),list) or |
There was a problem hiding this comment.
We don't use plain asserts in unit tests. Those could be removed if python is compiled with -o option.
numpy testing has a assert_ that can be used as replacement.
| kappa = [1] | ||
| for nn,m in enumerate(mnc[1:]): | ||
| n = nn+1 | ||
| kappa.append(m) |
There was a problem hiding this comment.
it might be enough to use a copy of m here, e.g. by making mnc into a copy of the argument mnc at the beginning of the function.
question is whether we want to support list of lists unchanged or convert to numpy asarray if mnc is not 1-dimensional.
There was a problem hiding this comment.
Given that multidimensional is not supported, we can just use ndarrays in the multidimensional case.
However, we cannot just replace mnc by a copy, kappa needs to have the copy, mnc needs to be the original in the loop below.
| #assert_equal(mvsk2mnc(mc2mvsk(mom[0])), mc2mnc(mom[0])) | ||
|
|
||
|
|
||
| multidimention_test = [np.array([[ 5., 10., 1.], |
| assert_equal(cum2mc(mnc2cum(mc2mnc(test_vals).T).T).T.astype(np.float32), test_vals) | ||
| assert_equal(cum2mc(mc2cum(test_vals).T).T.astype(np.float32), test_vals) | ||
| assert_equal(mvsk2mc(mc2mvsk(test_vals).T).T.astype(np.float32), test_vals) | ||
|
|
There was a problem hiding this comment.
another test here could compare the outcome one of the function in vectorized form with the list comprehension as I did in issue #2928
|
Also, based on brief browsing of the code: There might be only a few cases that don't work already correctly in a broadcasted, vectorized way. We need the unit tests to check which functions are actually wrong or overwrite the user provided object. |
|
@josef-pkt could you, please, describe what do you mean by "I think we can vectorize this without the loop in np.apply_along_axis"? I thought that we were dealing with N different vectors where every vector is a number of parameters from the different distribution. |
|
@zveryansky If each moment is an array, then we need to do the computation over all elements of the array. If all moments are in a multidimensional array, then the first loop selects the rows with moments and we have to do the computation for each element of the other dimensions. You are currently doing this "for each element of the other dimensions or axes" by an explicit loop using So, to fix this, we need unit tests for all cases, but most likely only minor adjustment to the current "automatic" elementwise solution. |
Fix moment helpers so that they do not do inplace modification Allow multidimensional input closes statsmodels#3362 closes statsmodels#2928
see #2928
Fixed the problem and add some tests.