2
\$\begingroup\$

I want to calculate the correlation time. First I calculate the auto-correlation function:

$$ \begin{align} \chi(t) = &\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t')m(t'+t)\\ &-\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t')\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t'+t)\\ &\sim e^{-t/\tau} \end{align} $$

The correlation-time can be calculated by fitting the autocorrelation function, i.e., \$\chi(t)\$ in exponential. For this I have written the following code:

def autocorrelation(M):
    start_time = time.time()
    tau = 1
    sweeps = len(M)
    auto = np.zeros(sweeps)
    for t in range(sweeps):
        some_time = sweeps-t
        first_term = np.average(M[:some_time]*M[t:sweeps])
        S1 = np.average(M[:some_time])
        S2 = np.average(M[t:sweeps])
        auto_temp = first_term - S1*S2
        if auto_temp > 0:
            auto[t] = auto_temp
        else:#remove oscillating part
            break 
    if auto[0] != 0:
        auto = auto[auto>0]
        auto = auto/auto[0] #normalization
        len_auto = len(auto)
        if len_auto > 1: #draw a straight line if you have atleast two points
            tau = int(-1/np.polyfit(np.arange(len_auto), np.log(auto), 1, w=np.sqrt(auto))[0])
    tau = max(tau,1)
    logging.info(f"Correlation time = {tau}")
    return tau

If you put in list M = m, the function will calculate autocorrelation function and return correlation time, i.e., \$\tau\$. I also thought of using NumPy's correlate and convolve, but I could not understand them well. I tried auto_convolve = numpy.convolve(m,m,mode = "same"), after normalization, the auto/auto[0] was not same as auto_convolve/auto_convolve[0].

\$\endgroup\$
3
  • \$\begingroup\$ Please can you check the equation. Why do you have \$\frac{1}{t_{max}-t}\$ twice in the second line of the equation and not just simplify to \$\frac{1}{(t_{max}-t)^2}\$. Additionally please use parentheses in situations like the above it's unclear what is inside the Sigma, is that a nested Sigma? \$\endgroup\$ Commented Jun 4, 2020 at 22:07
  • \$\begingroup\$ It's because there are two averages in the second term. I tried putting parenthesis, but it showed error: Your post appears to contain code that is not properly formatted as code. \$\endgroup\$ Commented Jun 5, 2020 at 1:50
  • \$\begingroup\$ The second term in expression of \$\chi(t)\$ is \$\bigg(\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t')\bigg)\bigg(\frac{1}{t_{max}-t}\sum\limits_{t'=0}^{t_{max}-t}m(t'+t)\bigg)\$. \$\endgroup\$ Commented Jun 5, 2020 at 3:20

1 Answer 1

1
\$\begingroup\$

Here are some general coding style suggestions.

Unused code

This variable is set but never used:

start_time = time.time()

It can be deleted, which means this line can also be deleted:

import time

Documentation

The PEP 8 style guide recommends adding docstrings for functions. Describe what the function does, what its input type is and what it returns:

def autocorrelation(M):
    """
    Calculate auto-correlation of ...
    M is a ... of type ...
    Returns a ... of type ...
    """

Naming

autocorrelation is easier to read as auto_correlation.

The variable name auto is not very descriptive. Similarly, using "temp" in a variable name (auto_temp) does not convey much meaning. The same for len_auto.

Comments

The comments that you added are helpful. Adding a few more would be nice.

Layout

The black program can be used to automatically add a little more consistent whitespace for better readability.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.