.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/markov_chain/plot_02_continuous_time_chain.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_api_gallery_probability_markov_chain_plot_02_continuous_time_chain.py: Kolmogorov's equations: a continuous-time Markov chain ======================================================== A machine is working (0), degraded (1), or broken (2), jumping between states at the rates in the generator :math:`Q`. The transition matrix :math:`P(t) = e^{tQ}` solves Kolmogorov's forward and backward equations and relaxes to the stationary distribution :math:`\pi`, the solution of :math:`\pi Q = 0`. .. GENERATED FROM PYTHON SOURCE LINES 13-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import ctmc_stationary_distribution, ctmc_transition_matrix Q = np.array( [ [-0.5, 0.4, 0.1], [0.0, -1.0, 1.0], [2.0, 0.0, -2.0], ] ) .. GENERATED FROM PYTHON SOURCE LINES 27-29 State probabilities starting from "working" ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-43 .. code-block:: Python times = np.linspace(0.0, 8.0, 200) probs = np.array([ctmc_transition_matrix(Q, t)[0] for t in times]) pi = ctmc_stationary_distribution(Q) print(f"stationary distribution: {pi.round(4)}") fig, ax = plt.subplots() for j, name in enumerate(["working", "degraded", "broken"]): (line,) = ax.plot(times, probs[:, j], label=name) ax.axhline(pi[j], color=line.get_color(), ls=":") ax.set_xlabel("t") ax.set_ylabel(r"$P_{0j}(t)$") ax.legend() ax.set_title(r"$P(t) = e^{tQ}$ converging to $\pi$") .. image-sg:: /api/gallery/probability/markov_chain/images/sphx_glr_plot_02_continuous_time_chain_001.png :alt: $P(t) = e^{tQ}$ converging to $\pi$ :srcset: /api/gallery/probability/markov_chain/images/sphx_glr_plot_02_continuous_time_chain_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none stationary distribution: [0.6061 0.2424 0.1515] Text(0.5, 1.0, '$P(t) = e^{tQ}$ converging to $\\pi$') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.032 seconds) .. _sphx_glr_download_api_gallery_probability_markov_chain_plot_02_continuous_time_chain.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_continuous_time_chain.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_continuous_time_chain.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_continuous_time_chain.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_