.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorial/plot_2_plot_spectral_laws.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_tutorial_plot_2_plot_spectral_laws.py: Plotting spectral laws ======================= In this section, we will briefly explain what is the spectral distribution of a random matrix and how to plot it in order to study its properties. Most ensembles spectral distribution are explained through spectral laws, that are going to be detailed as well. .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python # Author: Alejandro Santorum Varela # License: BSD 3-Clause .. GENERATED FROM PYTHON SOURCE LINES 16-27 Spectral distribution ------------------------------ Most of the information of a random matrix is explained through their eigenvalues. Random matrix eigenvalues or random matrix spectrum follows a certain distribution depending on the ensemble. We would like to study that distribution in order to have insight of the random matrix limiting behaviour. We can plot the eigenvalue density of any random matrix ensemble implemented in scikit-rmt. However, some technical details must be taken into account. .. GENERATED FROM PYTHON SOURCE LINES 29-31 As an example, we can try to plot the spectral density of the Gaussian Orthogonal Ensemble (GOE) in the interval (-10, 10). .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: Python from skrmt.ensemble import GaussianEnsemble goe = GaussianEnsemble(beta=1, n=1000) goe.plot_eigval_hist(bins=80, interval=(-10, 10), normalize=False) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_001.png :alt: Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-46 As we can see, the histogram does not explain barely anything about the ensemble. That is because we are not using correctly the theory behind the model. Most of the ensembles need their eigenvalues to be normalized by a certain constant in order to perceive their limiting density properly. By default, the scikit-rmt is implemented to normalize eigenvalues with the more suitable constant depending on the ensemble. .. GENERATED FROM PYTHON SOURCE LINES 48-51 In the former example, GOE matrices of size :math:`n \times n` should be normalized by :math:`1/\sqrt{n}`, which is done by default or by setting `normalize=True`. .. GENERATED FROM PYTHON SOURCE LINES 51-58 .. code-block:: Python import numpy as np from skrmt.ensemble import GaussianEnsemble goe = GaussianEnsemble(beta=1, n=1000) goe.plot_eigval_hist(bins=80, interval=(-10, 10), normalize=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_002.png :alt: Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-63 Additionally, we can control the plotting interval to get a better picture of the spectral density. In the GOE case, the spectral distribution is concentrated in the (-2,2) interval. Note that the user does *not* need to worry about the interval, **scikit-rmt** uses an adequate interval by default. .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: Python import numpy as np from skrmt.ensemble import GaussianEnsemble goe = GaussianEnsemble(beta=1, n=1000) goe.plot_eigval_hist(bins=80, interval=(-2, 2), normalize=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_003.png :alt: Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-73 This example would be equivalent for Gaussian Unitary Ensemble (GUE) and Gaussian Symplectic Ensemble (GSE). .. GENERATED FROM PYTHON SOURCE LINES 75-77 Another example would be using the Wishart Ensemble. In the following snippet of code Wishart Real Ensemble (WRE) is sampled. .. GENERATED FROM PYTHON SOURCE LINES 77-83 .. code-block:: Python from skrmt.ensemble import WishartEnsemble wre = WishartEnsemble(beta=1, p=1000, n=3000) wre.plot_eigval_hist(bins=80, interval=(-5,5)) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_004.png :alt: Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-86 By default, **scikit-rmt** takes charge of using the most suitable plotting  interval and eigenvalue normalization constant. .. GENERATED FROM PYTHON SOURCE LINES 86-92 .. code-block:: Python from skrmt.ensemble import WishartEnsemble wre = WishartEnsemble(beta=1, p=1000, n=3000) wre.plot_eigval_hist(bins=80) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_005.png :alt: Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-96 This example would be equivalent for Wishart Complex Ensemble (WCE) and Wishart Quaternion Ensemble (WQE), with the detail of controlling its representation interval. .. GENERATED FROM PYTHON SOURCE LINES 98-104 So far we have analyzed spectral distributions of GOE and WRE. In the first example it is easy to see that its eigenvalue distribution is quite similar to a semicircle. Thats because Gaussian Ensemble random matrices spectrum follow *Wigner's Semicircle Law*. In the second example, the shown density has a very particular shape too, it is known as the *Marchenko-Pastur Law*. But, what is a spectral law? .. GENERATED FROM PYTHON SOURCE LINES 106-124 Spectral laws -------------------------- Spectral laws define random matrix eigenvalue distribution when its size goes to infinity. So the spectral laws describes the limiting behavior of the spectrum of a random matrix ensemble. The most known spectral laws, and implemented by scikit-rmt, are the following: - **Winger Semicircle Law**: describes the limiting behaviour of Gaussian Ensemble random matrix spectrum. - **Marchenko-Pastur Law**: describes the limiting behaviour of Wishart Ensemble random matrix spectrum. - **Tracy-Widom Law**: describes the limiting behaviour of the largest eigenvalue of a Gaussian Ensemble random matrix. .. GENERATED FROM PYTHON SOURCE LINES 126-128 Anyone can forget about the specific plotting details and directly study those laws by using the functions provided in scikit-rmt. .. GENERATED FROM PYTHON SOURCE LINES 130-131 We can easily analyze **Wigner's Semicircle Law** as follows. .. GENERATED FROM PYTHON SOURCE LINES 131-137 .. code-block:: Python from skrmt.ensemble.spectral_law import WignerSemicircleDistribution wsd = WignerSemicircleDistribution(beta=1) wsd.plot_empirical_pdf(sample_size=2000, bins=80) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_006.png :alt: Wigner Semicircle Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-139 We can also study **Marchenko-Pastur Law** as by: .. GENERATED FROM PYTHON SOURCE LINES 139-145 .. code-block:: Python from skrmt.ensemble.spectral_law import MarchenkoPasturDistribution mpd = MarchenkoPasturDistribution(beta=1, ratio=1/3) mpd.plot_empirical_pdf(sample_size=2000, bins=80) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_007.png :alt: Marchenko-Pastur Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 146-147 Finally, **Tracy-Widom Law** can be represented using: .. GENERATED FROM PYTHON SOURCE LINES 147-153 .. code-block:: Python from skrmt.ensemble.spectral_law import TracyWidomDistribution twd = TracyWidomDistribution(beta=1) twd.plot_empirical_pdf(sample_size=2000, bins=80) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_008.png :alt: Tracy-Widom Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-162 Spectral laws analytical expression ----------------------------------- The spectral laws described so far have been proven to converge to certain analytical functions that defines the limiting behaviour of the eigenvalue distribution of the random matrices. The functions of scikit-rmt that are capable of plotting the spectral laws also support the representation of the theoretical eigenvalue pdf. .. GENERATED FROM PYTHON SOURCE LINES 164-167 The analytical probability function for the Gaussian Ensemble, known as Wigner Semicircle Law, supported on :math:`[-R, R]` and centered at :math:`(0,0)` is :math:`f(x) = \frac{2}{\pi R^2} \sqrt{R^2 - x^2}`. .. GENERATED FROM PYTHON SOURCE LINES 167-173 .. code-block:: Python from skrmt.ensemble.spectral_law import WignerSemicircleDistribution wsd = WignerSemicircleDistribution(beta=1) wsd.plot_empirical_pdf(sample_size=2000, bins=80, density=True, plot_law_pdf=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_009.png :alt: Wigner Semicircle Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 174-180 The analytical probability function for the Wishart Ensemble known as Marchenko-Pastur Law with parameter :math:`\lambda = p/n \in (0,1]` is :math:`f_{\lambda}(x) = \frac{1}{2\pi \sigma^2}\frac{\sqrt{(\lambda_+ - x)(x - \lambda_-)}}{\lambda x}`, where :math:`\lambda_{\pm} = \sigma^2 (1 \pm \sqrt{\lambda})^2`. If :math:`\lambda > 1` then the limiting distribution has an additional mass probability point in the origin of size :math:`1 - \frac{1}{\lambda}`. .. GENERATED FROM PYTHON SOURCE LINES 180-186 .. code-block:: Python from skrmt.ensemble.spectral_law import MarchenkoPasturDistribution mpd = MarchenkoPasturDistribution(beta=1, ratio=1/3) mpd.plot_empirical_pdf(sample_size=2000, bins=80, density=True, plot_law_pdf=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_010.png :alt: Marchenko-Pastur Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 187-194 In the other hand, the Tracy-Widom Law has a complex analytical expression, that is the solution of a particular non-linear differential equation, described in detail in: S. Bauman. "The Tracy-Widom Distribution and its Application to Statistical Physics". MIT Department of Physics. 2017. The package scikit-rmt represents a precise approximation of the theoretical Tracy-Widom pdf. .. GENERATED FROM PYTHON SOURCE LINES 194-200 .. code-block:: Python from skrmt.ensemble.spectral_law import TracyWidomDistribution twd = TracyWidomDistribution(beta=1) twd.plot_empirical_pdf(sample_size=2000, bins=80, density=True, plot_law_pdf=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_011.png :alt: Tracy-Widom Law - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 201-205 Finally, the limiting distribution of the Manova Ensemble is not described by any famous Law, but its expression has been determined. This library provides functionality to show it on top of the empirical histogram of the eigenvalue spectrum. .. GENERATED FROM PYTHON SOURCE LINES 205-210 .. code-block:: Python from skrmt.ensemble.spectral_law import ManovaSpectrumDistribution msd = ManovaSpectrumDistribution(beta=1, ratio_a=3, ratio_b=3) msd.plot_empirical_pdf(sample_size=2000, bins=80, density=True, plot_law_pdf=True) .. image-sg:: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_012.png :alt: Manova Spectrum - Eigenvalue histogram :srcset: /auto_tutorial/images/sphx_glr_plot_2_plot_spectral_laws_012.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.775 seconds) .. _sphx_glr_download_auto_tutorial_plot_2_plot_spectral_laws.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2_plot_spectral_laws.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2_plot_spectral_laws.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_2_plot_spectral_laws.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_