Data smoothing (interpolation), Cubic and SPline
Source code name: "stat_smoothing_spline.py"
Programming language: Python
Topic: Statistics/Smoothing
DMelt Version 1. Last modified: 12/11/2015. License: Pro
https://datamelt.org/code/cache/stat_smoothing_spline_3216.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


# Here we use weightied smoothing as given by a histogram
 
from java.awt import Color,Font
from java.util import Random
from jhplot import *
from jhplot.stat import Interpolator

c1 = HPlot("Canvas",600,400)
c1.setGTitle("Cubic Spline interpolation")
c1.visible()
c1.setRange(-2,3,0,500)

h2 = H1D("Background",60, -2.0, 3.0)
h2.setFill(1)
h2.setErrX(0)
h2.setErrY(1)
h2.setFillColor(Color.yellow)
h2.setColor(Color.green)
h2.setErrColorY(Color.blue)

r=Random()
for i in range(10000): 
      h2.fill(r.nextGaussian()-2)
      if (i<3000): h2.fill(r.nextGaussian()*0.3+1)
      if (i<2000): h2.fill(r.nextGaussian()*0.7+2)

c1.draw(h2)


k1=Interpolator(h2)
k2=Interpolator(h2)
s1=k1.interpolateCubicSpline(0.9999,2) 
s2=k2.interpolateCubicSpline(0.9,2) 
p1=P1D("CubicSpline. s=0.9999")
p2=P1D("CubicSpline. s=0.9 ")
fit=s1.getSplinePolynomials() 
print "Length=",len(fit)
nbins=100
max=3
min=-2
h=(max-min)/float(nbins)
print "Step=",h
for i in range(nbins):
           z1 = min + i * h; 
           z2=s1.evaluate(z1)
           z3=s2.evaluate(z1)
           print i,z1, z2
           p1.add(z1,z2)
           p2.add(z1,z3)
p1.setStyle("l")
p1.setSymbolSize(1)
p1.setColor(Color.red)
c1.draw(p1)

p2.setStyle("l")
p2.setSymbolSize(1)
p2.setColor(Color.blue)
c1.draw(p2)


You see the box below because you did not login.