.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/statistical_tests/plot_kolmogorov_pvalue.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_data_analysis_statistical_tests_plot_kolmogorov_pvalue.py: Kolmogorov-Smirnov : understand the p-value =========================================== .. GENERATED FROM PYTHON SOURCE LINES 7-13 In this example, we illustrate the calculation of the Kolmogorov-Smirnov p-value. * We generate a sample from a gaussian distribution. * We create a Uniform distribution with known parameters. * The Kolmogorov-Smirnov statistics is computed and plot on the empirical cumulated distribution function. * We plot the p-value as the area under the part of the curve exceeding the observed statistics. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 22-23 We generate a sample from a standard gaussian distribution. .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python dist = ot.Normal() samplesize = 10 sample = dist.getSample(samplesize) .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python testdistribution = ot.Normal() result = ot.FittingTest.Kolmogorov(sample, testdistribution, 0.01) .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python pvalue = result.getPValue() pvalue .. rst-class:: sphx-glr-script-out .. code-block:: none 0.8487394657394757 .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python KSstat = result.getStatistic() KSstat .. rst-class:: sphx-glr-script-out .. code-block:: none 0.179679242488139 .. GENERATED FROM PYTHON SOURCE LINES 43-44 Compute exact Kolmogorov PDF. .. GENERATED FROM PYTHON SOURCE LINES 46-47 Create a function which returns the CDF given the KS distance. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python def pKolmogorovPy(x): y = ot.DistFunc.pKolmogorov(samplesize, x[0]) return [y] .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python pKolmogorov = ot.PythonFunction(1, 1, pKolmogorovPy) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Create a function which returns the KS PDF given the KS distance: use the `gradient` method. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python def kolmogorovPDF(x): return pKolmogorov.gradient(x)[0, 0] .. GENERATED FROM PYTHON SOURCE LINES 69-84 .. code-block:: Python def dKolmogorov(x, samplesize): """ Compute Kolmogorov PDF for given x. x : a Sample, the points where the PDF must be evaluated samplesize : the size of the sample Reference Numerical Derivatives in Scilab, Michael Baudin, May 2009 """ n = x.getSize() y = ot.Sample(n, 1) for i in range(n): y[i, 0] = kolmogorovPDF(x[i]) return y .. GENERATED FROM PYTHON SOURCE LINES 85-94 .. code-block:: Python def linearSample(xmin, xmax, npoints): """Returns a sample created from a regular grid from xmin to xmax with npoints points.""" step = (xmax - xmin) / (npoints - 1) rg = ot.RegularGrid(xmin, step, npoints) vertices = rg.getVertices() return vertices .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: Python n = 1000 # Number of points in the plot s = linearSample(0.001, 0.999, n) y = dKolmogorov(s, samplesize) .. GENERATED FROM PYTHON SOURCE LINES 101-116 .. code-block:: Python def drawInTheBounds(vLow, vUp, n_test): """ Draw the area within the bounds. """ palette = ot.Drawable.BuildDefaultPalette(2) myPaletteColor = palette[1] polyData = [[vLow[i], vLow[i + 1], vUp[i + 1], vUp[i]] for i in range(n_test - 1)] polygonList = [ ot.Polygon(polyData[i], myPaletteColor, myPaletteColor) for i in range(n_test - 1) ] boundsPoly = ot.PolygonArray(polygonList) return boundsPoly .. GENERATED FROM PYTHON SOURCE LINES 117-118 Create a regular grid starting from the observed KS statistics. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: Python nplot = 100 x = linearSample(KSstat, 0.6, nplot) .. GENERATED FROM PYTHON SOURCE LINES 124-125 Compute the bounds to fill: the lower vertical bound is zero and the upper vertical bound is the KS PDF. .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: Python vLow = [[x[i, 0], 0.0] for i in range(nplot)] vUp = [[x[i, 0], pKolmogorov.gradient(x[i])[0, 0]] for i in range(nplot)] .. GENERATED FROM PYTHON SOURCE LINES 131-153 .. code-block:: Python boundsPoly = drawInTheBounds(vLow, vUp, nplot) boundsPoly.setLegend("pvalue = %.4f" % (pvalue)) curve = ot.Curve(s, y) curve.setLegend("Exact distribution") curveStat = ot.Curve([KSstat, KSstat], [0.0, kolmogorovPDF([KSstat])]) curveStat.setColor("red") curveStat.setLegend("KS-statistics = %.4f" % (KSstat)) graph = ot.Graph( "Kolmogorov-Smirnov distribution (known parameters)", "KS-Statistics", "PDF", True, "upper right", ) graph.setLegends(["Empirical distribution"]) graph.add(curve) graph.add(curveStat) graph.add(boundsPoly) graph.setTitle("Kolmogorov-Smirnov distribution (known parameters)") view = viewer.View(graph) plt.show() .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_kolmogorov_pvalue_001.png :alt: Kolmogorov-Smirnov distribution (known parameters) :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_kolmogorov_pvalue_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-156 We observe that the p-value is the area of the curve which corresponds to the KS distances greater than the observed KS statistics. .. _sphx_glr_download_auto_data_analysis_statistical_tests_plot_kolmogorov_pvalue.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kolmogorov_pvalue.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kolmogorov_pvalue.py `