question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

autocorr vs autocorrplot

See original GitHub issue

This issue is actually more of a question, it is quite possible that we can resolve this without actual code changes. arviz maintains autocorr code in stats.diagnostics._autocorr and plots.autocorrplot (both are inherited from the pymc3 codebase). Both here and in pymc3 the way the autocorrelation is computed differs, in particular plots.autocorrplot uses np.correlate(y, y, mode=2) whereas stats.diagnostics.autocorr uses fftconvolve(y, y[::-1]). Is there a reason why plots.autocorrplot does not simply use stats.diagnostics.autocorr under the hood?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
ColCarrollcommented, Jul 24, 2018

Ooh, maybe the opposite of historical reasons. I believe I was porting this to use xarray, and wanted to avoid plt.acorr, which used to be used: it does not add much to the code complexity, but allows a lot more flexibility in signature and styling. I did not even notice az.diagnostics._autocorr!

Checking out benchmarks and actual values, it looks like the two functions are not identical, but are quite close where there is actual autocorrelation, and that az.diagnostics._autocorr is much faster for large inputs, and modestly slower for small ones. I attached the code below. Trying np.correlate on 1e6 points scares my computer.

image

def numpy_version(y):
    points = len(y)
    z = np.correlate(y, y, mode=2)[points - 1:]
    return z / z[0]

metrics = []
for j in range(2, 6):
    points = 10 ** j
    y = np.random.randn(points)
    for lag in (0, 1, 10, 20):
        metrics.append({'points': points, 'lag': lag})
        z = y.copy()
        if lag:
            z[:-lag] = z[:-lag] + z[lag:]
        t0 = time.time()
        a = numpy_version(z)
        metrics[-1]['numpy_version'] = time.time() - t0
        t0 = time.time()
        b = az.diagnostics._autocorr(z)
        metrics[-1]['arviz_version'] = time.time() - t0
        metrics[-1]['difference'] = np.linalg.norm(a - b)
1reaction
MFreidankcommented, Jul 24, 2018

@junpenglao yes it is from pymc3. I noticed that it gives the same results as pystan. Thanks for providing background on the implementation and theory. I think it makes sense to use this method to compute autocorrelation in az.plots.autocorrplot as well. I will provide a pull request for that tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question about autocorrelation_plot result vs autocorr result
You are using both functions correctly, but... Autocorrelation_plot uses a different way of calculating autocorrelations then autocorr() ...
Read more >
How to interpret autocorrelation plot in MCMC - Cross Validated
First of all: if memory and computational time for handling the MCMC output are not limiting, thinning is never "optimal".
Read more >
The BCHOICE Procedure - ODS Graphics - SAS Help Center
ODS Graph Name Plot Description Statement and Option ADPanel Autocorrelation function and density panel PLOTS=(AUTOCORR DENSI... AutocorrPanel Autocorrelation function panel PLOTS=AUTOCORR AutocorrPlot Autocorrelation function plot PLOTS(UNPACK)=AUTOCO......
Read more >
arviz.plots.autocorrplot — ArviZ 0.12.1 documentation
Source code for arviz.plots.autocorrplot. """Autocorrelation plot of data. ... """Bar plot of the autocorrelation function for a sequence of data.
Read more >
Plots — PyMC3 3.11.5 documentation
plots module are available through pymc3.<function> or pymc3.plots.<function> , but for their API documentation please refer to the ArviZ documentation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found