Non parametric distribution fittingΒΆ

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

[22]:
from __future__ import print_function
import openturns as ot
[23]:
# Create data
ot.RandomGenerator.SetSeed(0)
distribution = ot.Gamma(6.0, 1.0)
sample = distribution.getSample(800)
[24]:
# Estimate the Spearman correlation
kernel = ot.KernelSmoothing()
estimated = kernel.build(sample)
[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
[25]:
../../_images/examples_data_analysis_estimate_non_parametric_distribution_5_0.svg
[26]:
# access the bandwidth
kernel.getBandwidth()
[26]:

[0.484017]

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

[0.639633]

[28]:
# Regenerate with another bandwidth rule
estimated = kernel.build(sample, bandwidth)
[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
[29]:
../../_images/examples_data_analysis_estimate_non_parametric_distribution_9_0.svg