Non parametric distribution fittingΒΆ

In this example we are going to estimate a non parametric distribution using the kernel smoothing method.

In [22]:
from __future__ import print_function
import openturns as ot
In [23]:
# Create data
ot.RandomGenerator.SetSeed(0)
distribution = ot.Gamma(6.0, 1.0)
sample = distribution.getSample(800)
In [24]:
# Estimate the Spearman correlation
kernel = ot.KernelSmoothing()
estimated = kernel.build(sample)
In [25]:
# Plot original distribution vs kernel smoothing
graph = ot.Graph()
graph.setTitle('Kernel smoothing vs original')
graph.add(distribution.drawPDF())
kernel_plot = estimated.drawPDF().getDrawable(0)
kernel_plot.setColor('blue')
graph.add(kernel_plot)
graph.setLegends(['original', 'KS'])
graph.setLegendPosition('topright')
graph
Out[25]:
../../_images/examples_data_analysis_estimate_non_parametric_distribution_5_0.svg
In [26]:
# access the bandwidth
kernel.getBandwidth()
Out[26]:

[0.484017]

In [27]:
# Compute bandwitdh with the Silverman rule
bandwidth = kernel.computeSilvermanBandwidth(sample)
bandwidth
Out[27]:

[0.639633]

In [28]:
# Regenerate with another bandwidth rule
estimated = kernel.build(sample, bandwidth)
In [29]:
# Plot original distribution vs kernel smoothing
graph = ot.Graph()
graph.setTitle('Kernel smoothing vs original')
graph.add(distribution.drawPDF())
kernel_plot = estimated.drawPDF().getDrawable(0)
kernel_plot.setColor('blue')
graph.add(kernel_plot)
graph.setLegends(['original', 'KS'])
graph.setLegendPosition('topright')
graph
Out[29]:
../../_images/examples_data_analysis_estimate_non_parametric_distribution_9_0.svg