2 General class for analyzing autocorrelations.
7 Analysis class for autocorrelation of data.
9 Let \f$ X \left( t \right) \f$ be a function of time \f$ t \f$, which is
10 known at evenly-spaced points in time; that is, one knows
11 \f$ X_n = X \left( n \Delta t \right) \f$ for
12 \f$ n \in \left[0, n_{\textrm{max}} \right] \f$.
14 This class calculates the autocorrelation function
15 \f[ C \left( N \right) =
16 \left< X \left( 0 \right) \cdot X \left( N \Delta t \right) \right> =
17 \frac{1}{n_{\textrm{max}} + 1 - N}
18 \sum_{i = 0}^{n_{\textrm{max}} - N} X_i X_{i + N}
20 for arbitrary \f$ N \in \left[0, N_{\textrm{max}} \right] \f$, where
21 \f$ N_{\textrm{max}} \f$ is specified upon construction of an instance of
24 The argument \f$ N \f$ to the autocorrelation function will be called
25 "correlation time" in the context of this class. It is measured in units of
34 Throws if any of the arguments have invalid types.
36 Throws if any of the arguments have invalid values.
38 @param[in] maxCorrelationTime
39 The maximal correlation time to measure,
40 \f$ N_{\textrm{max}} \f$, which must be a non-negative `int`.
43 if not isinstance(maxCorrelationTime, int):
46 if maxCorrelationTime < 0:
54 from .OnTheFlyStatisticsDDDA
import OnTheFlyStatisticsDDDA
55 for _
in range(0, maxCorrelationTime + 1):
62 Returns the maximal correlation time \f$ N_{\textrm{max}} \f$.
70 Returns whether enough data have been supplied for there to be data on
71 the correlation function with the given correlation time \f$ N \f$.
73 One needs to supply at least \f$ N + 1 \f$ data points before one can
74 query the correlation function for \f$ N \f$.
77 Throws if any of the arguments have invalid types.
79 Throws if any of the arguments have invalid values.
81 @param[in] correlationTime
82 The correlation time \f$ N \f$ as an `int` in the range
83 `[0, self.getMaxCorrelationTime()]`.
86 if not isinstance(correlationTime, int):
89 if correlationTime < 0:
96 return self.
_correlations[0].getSampleSize() > correlationTime
101 Returns an `OnTheFlyStatisticsDDDA` object that holds information on the
102 sample of measured autocorrelations \f$ C \left( N \right) \f$ for the
103 given correlation time `correlationTime`, \f$ N \f$.
106 Throws if any of the arguments have invalid types.
108 Throws if any of the arguments have invalid values.
110 Throws if `not self.correlationTimeIsAvailable(correlationTime)`.
112 @param[in] correlationTime
113 The correlation time \f$ N \f$ to return results for, as an
114 `int` in the range `[0, self.getMaxCorrelationTime()]`. Also,
115 `self.correlationTimeIsAvailable(correlationTime)` must
116 return `True` for this call to be valid.
119 if not isinstance(correlationTime, int):
122 if correlationTime < 0:
134 def addDatum(self, datum, multiplicator = None):
136 Supplies a new datum \f$ X_i \f$, where \f$ i \f$ is implied to be the
137 number of times `addDatum` has been called previously.
140 The datum to add, which must be compatible with the
142 @param[in] multiplicator
143 A callable that takes two variables, let them be called
144 \f$ X_i \f$ and \f$ X_j \f$, which are of the type that
145 `datum` is an instance of, and returns their product
146 \f$ X_i \cdot X_j \f$ as a type that is compatible with
147 `OnTheFlyStatisticsDDDA.addDatum`.
148 If `None` is given, the default multiplication operator
149 (`datum.__mul__`) is used.
150 No guarantee is given regarding the order of the operands of
151 the multiplication operator.
154 def defaultMultiplicator(first, second):
155 return first.__mul__(second)
157 if multiplicator
is None:
158 multiplicator = defaultMultiplicator
164 multiplied = multiplicator(datum, self.
_dataBuffers[N][0])
171 def getMPLAxes(self, showEstimatedStandardDeviation = True):
173 Returns an `matplotlib.axes.Axes` object that plots the autocorrelation
174 function against the correlation time.
177 Throws if any of the arguments have invalid types.
179 Throws if any of the arguments have invalid values.
181 @param[in] showEstimatedStandardDeviation
182 Whether to show, for each data point, the estimated standard
186 if not isinstance(showEstimatedStandardDeviation, bool):
190 import matplotlib.figure
192 figure = matplotlib.figure.Figure()
193 axes = figure.add_subplot(1, 1, 1)
205 values.append(autocorrelation.getSampleMean())
206 if showEstimatedStandardDeviation:
208 autocorrelation.getOptimalStandardErrorOfTheMean())
210 if len(errorbars) == 0:
214 axes.errorbar(times, values, yerr = errorbars)
216 axes.set_xlabel(
r'Correlation Time $ N $')
217 axes.set_ylabel(
r'$ C(N) $')