Automatic quantification of a marginal based on a 1D sample

Hello all,

I propose you the following python script. It allows to simply quantify a 1D variable from a sample. First, you create the family of parametric distribution on which you want to test your sample. If the best distribution does not fulfill the quality criteria, you go to a non parametric quantification by Kernel Smoothing. The output of the function is the distribution and the value of the test.

# -*- coding: iso-8859-15 -*-
from openturns import *
def quantification(sample1D):
  # Creation of a factory collection
  factoryCollection = FactoryCollection()
    BestDistributionMarg = Distribution(FittingTest().BestModelKolmogorov(sample1D, factoryCollection))
    print BestDistributionMarg
    print "impossible to do the parametric quantification"
## Study of the result by comparing the empirical law of the sample and the distribution obtained by parametric
## if the test is ok, we keep the parametric law, if not, go to non parametric quantification
    TestMarg = FittingTest_Kolmogorov(SampleInputMarg,BestDistributionMarg).getBinaryQualityMeasure()
    print "Parametric quantification"
    TestMarg = False
## test not ok
  if TestMarg == False :
    BestDistributionMarg = Distribution(kernel.buildImplementation(sample1D))
    print "Quantification by kernel smoothing"
  ## optional visual tests
  QQPlot = VisualTest().DrawQQplot(sample1D,BestDistributionMarg,1000)
  QQPlot.draw("QQPlotMargin", 640, 480)
  return BestDistributionMarg, TestMarg


1. souchaud@… -- 2011-04-22 15:28

Hello all, Just be careful of the version of OT you use with this script : from the 0.13.2rc2 you should replace "FactoryCollection" by "DistributionFactoryCollection".

2. souchaud@… -- 2011-04-22 15:28

Hello Anne-Laure,

Thank you for this information. It will help me modify my scripts.

Have a nice day!