Skip to content

Iss2928 fix stats.moment_helpers inplace modification - #3362

Closed
zverianskii wants to merge 8 commits into
statsmodels:masterfrom
zverianskii:iss2928
Closed

zverianskii wants to merge 8 commits into
statsmodels:masterfrom
zverianskii:iss2928

Conversation

@zverianskii

@zverianskii zverianskii commented Jan 6, 2017 •

Copy link
Copy Markdown
Contributor

see #2928

Fixed the problem and add some tests.

zveryansky added 8 commits January 5, 2017 18:02
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.001%) to 88.027% when pulling b5cad45 on zveryansky:iss2928 into 22f2766 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

@zveryansky Thanks
However, I think we can vectorize this without the loop in np.apply_along_axis.
I don't remember the details, but I think there are no inherent problems in the computation that would make a broadcasting, vectorized solution impossible.

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo multidimension t->s

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another test here could compare the outcome one of the function in vectorized form with the list comprehension as I did in issue #2928

@josef-pkt

Copy link
Copy Markdown
Member

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.

@zverianskii

Copy link
Copy Markdown
Contributor Author

@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.

@josef-pkt

Copy link
Copy Markdown
Member

@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 apply_along_axis. However, the default behavior of numpy's elementwise computation is already doing it.
But, at least in mnc2cum(m) this is buggy because we are changing the underlying data during the loop because of the inplace operation without having made a copy.

So, to fix this, we need unit tests for all cases, but most likely only minor adjustment to the current "automatic" elementwise solution.

@josef-pkt josef-pkt changed the title Iss2928 Iss2928 fix stats.moment_helpers inplace modification Feb 26, 2017
@bashtage bashtage self-assigned this May 2, 2019
bashtage pushed a commit to bashtage/statsmodels that referenced this pull request May 9, 2019
Fix moment helpers so that they do not do inplace modification
Allow multidimensional input

closes statsmodels#3362
closes statsmodels#2928
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants