Skip to content

ENH: Improve numerical stability of _ar_transparams, _ar_invtransparams - #5569

Merged
bashtage merged 2 commits into
statsmodels:masterfrom
jcdang:master
May 7, 2019
Merged

bashtage merged 2 commits into
statsmodels:masterfrom
jcdang:master

Conversation

@jcdang

@jcdang jcdang commented Apr 17, 2019

Copy link
Copy Markdown
Contributor

(1 - e^-x)/(1 + e^-x) = tanh(x/2)

I modified the code to use numpy's tanh since it doesn't have the issue with large scale numbers.

@coveralls

coveralls commented Apr 17, 2019 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 84.533% when pulling 4817483 on jcdang:master into 599c762 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member

Thanks! Would you mind also updating _ar_invtransparams to use arctanh?

@jcdang

jcdang commented Apr 18, 2019

Copy link
Copy Markdown
Contributor Author

Thanks! Would you mind also updating _ar_invtransparams to use arctanh?

I can update that specific part of the code but I don't think _ar_invtransparams looks correct as a whole. This is questionable.

@jseabold Can you help?

This my proposed change:

    invarcoefs = -np.log((1-params)/(1+params))

to

    invarcoefs = 2*np.arctanh(params)

@ChadFulton

Copy link
Copy Markdown
Member

That looks good to me. The code you referenced is a little less clear than it could be, but it seems to be computing the partial autocorrelations just fine, which is what it is intended to do.

@codecov

codecov Bot commented Apr 19, 2019

Copy link
Copy Markdown

Codecov Report

Merging #5569 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5569      +/-   ##
==========================================
+ Coverage   82.03%   82.03%   +<.01%     
==========================================
  Files         588      588              
  Lines       93061    93064       +3     
  Branches    10317    10317              
==========================================
+ Hits        76344    76347       +3     
  Misses      14355    14355              
  Partials     2362     2362
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_tsa_tools.py 99.8% <100%> (ø) ⬆️
statsmodels/tsa/tsatools.py 89.78% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 599c762...4817483. Read the comment docs.

1 similar comment
@codecov

codecov Bot commented Apr 19, 2019

Copy link
Copy Markdown

Codecov Report

Merging #5569 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5569      +/-   ##
==========================================
+ Coverage   82.03%   82.03%   +<.01%     
==========================================
  Files         588      588              
  Lines       93061    93064       +3     
  Branches    10317    10317              
==========================================
+ Hits        76344    76347       +3     
  Misses      14355    14355              
  Partials     2362     2362
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_tsa_tools.py 99.8% <100%> (ø) ⬆️
statsmodels/tsa/tsatools.py 89.78% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 599c762...4817483. Read the comment docs.

@jcdang

jcdang commented Apr 19, 2019

Copy link
Copy Markdown
Contributor Author

That looks good to me. The code you referenced is a little less clear than it could be, but it seems to be computing the partial autocorrelations just fine, which is what it is intended to do.

I can tell you for a fact that _ar_transparams and _ar_invtransparams are not inverses of each other (both before and after the hyperbolic tangent changes). I was unable to write a unit test that covers the inverse transparams because I'm uncertain how it is suppose to work.

@ChadFulton

Copy link
Copy Markdown
Member

I can tell you for a fact that _ar_transparams and _ar_invtransparams are not inverses of each other (both before and after the hyperbolic tangent changes).

Can you expand on this? In practice, they seem to work as inverses.

import numpy as np
from numpy.testing import assert_allclose
from statsmodels.tsa.tsatools import _ar_transparams, _ar_invtransparams

ar_params = np.r_[0.3, 0.2123, -0.1]
assert_allclose(ar_params, _ar_invtransparams(_ar_transparams(ar_params)))

@jcdang

jcdang commented Apr 19, 2019 •

Copy link
Copy Markdown
Contributor Author

Here's one of the param inputs I caught while running ARIMA:

ar_params = np.r_[63.07415068, -4.5933353, -6.1342963, -7.82067222, -9.73510476, -12.0269079, -14.95639924]

It seems to be okay for params between 0 and 1 but larger numbers will throw it off

Maybe what I'm using is not a valid test scenario?

import statsmodels.api as sm
import numpy as np

values = np.array([x / 100.0 for x in range(0, 20)])
mod = sm.tsa.ARIMA(values, order=(7, 0, 0))
res = mod.fit()

@ChadFulton

Copy link
Copy Markdown
Member

I see, thanks for the example. After applying _ar_transparams(ar_params), the transformed parameters are right at the boundary of the transformation space (i.e. the roots of the associated lag polynomial are all very close to 1).

ar_params = np.r_[63.07415068, -4.5933353, -6.1342963, -7.82067222, -9.73510476, -12.0269079, -14.95639924]
transformed = _ar_transparams(ar_params.copy())
p = np.polynomial.Polynomial(np.r_[1, -transformed])
print(np.abs(1 / p.roots()))

yields:

[0.9999994298 0.9999999656 0.9999999656 1.           1.           1.           1.          ]

I think probably the issue is that we're losing some precision somewhere.

It's not surprising that it pops up in your example, since the transformation is to require the parameters to correspond to a stationary process, while the process that you are fitting is definitely non-stationary.

The version of the transformation in the state space library (sm.tsa.statespace.tools.constrain_stationary_univariate and sm.tsa.statespace.tools.unconstrain_stationary_univariate) work somewhat better in this particular example, but they're still not perfect.

@ChadFulton ChadFulton changed the title Fixed bug where AR coefficients were coming out to NaN ENH: Improve numerical stability of _ar_transparams, _ar_invtransparams Apr 21, 2019
@bashtage

bashtage commented May 7, 2019

Copy link
Copy Markdown
Member

@jcdang Any chance you could implement the other direction and then we can get this merged for the upcoming release?

@bashtage bashtage self-assigned this May 7, 2019
@jcdang

jcdang commented May 7, 2019

Copy link
Copy Markdown
Contributor Author

@bashtage It's implemented already. I didn't write a test for it. Do you want me to add the composed inverse test @ChadFulton gave?

@bashtage

bashtage commented May 7, 2019

Copy link
Copy Markdown
Member

@jcdang Nope, goo to go then.

@bashtage
bashtage merged commit 6a9025b into statsmodels:master May 7, 2019
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