.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_temporalTimeSeriesAnalysis/plot_autoregressiveAutocovariance.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_autoregressiveAutocovariance.py: AR(1) analytical and estimated autocovariance ============================================= .. 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-24 .. code-block:: Python srate = 1 T = 10000 sigma = 1.0 phi = -0.9 lags = np.arange(20) .. GENERATED FROM PYTHON SOURCE LINES 25-28 Create white noise ------------------ .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. 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 34-37 Create autoregressive time series --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python ar = np.empty(len(w), dtype=np.double) ar[0] = w[0] for i in range(1, len(w)): ar[i] = phi * ar[i-1] + w[i] .. GENERATED FROM PYTHON SOURCE LINES 43-46 Estimate autocovariance ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-60 .. code-block:: Python estAcov = np.zeros(len(lags)) anaAcov = np.zeros(len(lags)) arMu = np.mean(ar) for h in lags: if h > 0: xs = ar[h:] - arMu xt = ar[:-h] - arMu elif h == 0: xs = ar - arMu xt = ar - arMu estAcov[h] = np.mean(xs * xt) anaAcov[h] = phi**h * sigma**2 / (1 - phi**2) .. GENERATED FROM PYTHON SOURCE LINES 61-64 Plot autoregressive time series, true and estimated autocovariance ------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 64-91 .. code-block:: Python fig = go.Figure() trace = go.Scatter(x=time, y=ar, mode="lines+markers") fig.add_trace(trace) fig.update_layout(xaxis=dict(title="Time (sec)"), yaxis=dict(title="x")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html(f"figures/autoregressiveSamplesN{T}.html") fig.write_image(f"figures/autoregressiveSamplesN{T}.png") fig = go.Figure() trace = go.Scatter(x=lags, y=anaAcov, mode="lines+markers", name="analytical") fig.add_trace(trace) trace = go.Scatter(x=lags, y=estAcov, mode="lines+markers", name="estimated") fig.add_trace(trace) fig.update_layout(title=f"N={T}", xaxis=dict(title="Lag (samples)"), yaxis=dict(title="Autocovariance")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html(f"figures/autoregressiveAutoCovN{T}.html") fig.write_image(f"figures/autoregressiveAutoCovN{T}.png") fig .. raw:: html


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