.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_temporalTimeSeriesAnalysis/plot_whiteNoiseAutocorrelation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_temporalTimeSeriesAnalysis_plot_whiteNoiseAutocorrelation.py: White noise analytical and estimated autocorrelation, and 95% confidence interval ================================================================================= .. GENERATED FROM PYTHON SOURCE LINES 7-9 Import requirements ------------------- .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import os import numpy as np import plotly.graph_objects as go .. GENERATED FROM PYTHON SOURCE LINES 15-17 Define variables ----------------- .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python srate = 1 T = 10000 sigma = 1.0 lags = np.arange(20) .. GENERATED FROM PYTHON SOURCE LINES 24-27 Create white noise ------------------ .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: Python time = np.arange(0, T, 1.0/srate) N = len(time) w = np.random.normal(loc=0, scale=sigma, size=N) .. GENERATED FROM PYTHON SOURCE LINES 33-36 Estimate autocorrelation ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 36-50 .. code-block:: Python anaAcor = np.zeros(len(lags)) anaAcor[0] = sigma estAcor = np.zeros(len(lags)) wMu = np.mean(w) wSTD = np.std(w) for h in lags: if h == 0: estAcor[h] = 1.0 else: xs = w[h:] - wMu xt = w[:-h] - wMu estAcor[h] = np.mean(xs * xt)/wSTD .. GENERATED FROM PYTHON SOURCE LINES 51-54 Plot white noise analytical and estimated autocorrelations plus 95% confidence interval --------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-72 .. code-block:: Python fig = go.Figure() trace = go.Scatter(x=lags, y=anaAcor, mode="lines+markers", name="analytical") fig.add_trace(trace) trace = go.Scatter(x=lags, y=estAcor, mode="lines+markers", name="estimated") fig.add_trace(trace) fig.add_hline(y=1.96/np.sqrt(N), line=dict(dash="dash")) fig.add_hline(y=-1.96/np.sqrt(N), line=dict(dash="dash")) fig.update_layout(title=f"N={T}", xaxis=dict(title="Lag (samples)"), yaxis=dict(title="Autocorrelation")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html(f"figures/whiteNoiseAutoCorN{T}.html") fig.write_image(f"figures/whiteNoiseAutoCorN{T}.png") fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.096 seconds) .. _sphx_glr_download_auto_examples_01_temporalTimeSeriesAnalysis_plot_whiteNoiseAutocorrelation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_whiteNoiseAutocorrelation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_whiteNoiseAutocorrelation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_whiteNoiseAutocorrelation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_