A (experimental) Julia package for fitting Smoothing Splines of degrees
where
where
From the optimality conditions of the optimization problem it can be shown that the coefficients
which is the system of equations that this package end up solving. The optimal value of
In additional to the standard spline fit, the package provides the possibility of constraining various aspects of the spline,
where
using Plots, MLJ, LinearAlgebra, GeneralizedSmoothingSplines
n = 100 # number of samples
sigma = 0.2 # noise standard deviation
a,b = -π,π # interval [a,b]
delta = b - a # Interval width
# Simulating Data
X = a .+ sort(rand(n))*delta
f(t) = 1.0 ./ (1.0 .+ exp.(-5.0*t))
y = f(X) + sigma*randn(length(X))
# Creating 10 data points to predict
Xnew = a .+ sort(rand(10))*delta
# Fitting Smoothing Spline
spl = SmoothingSpline()
mach = machine(spl,X,y)
tune!(mach)
preds = predict(mach,Xnew)
# Fitting Shape Restricted Spline
bounds = (0.0,1,0.0) # Min/Max/Strictly increasing
res_spl = SmoothingSpline(shape_restrictions=(:lowerbound,:upperbound,:increasing),bounds=bounds)
res_mach = machine(res_spl,X,y)
tune!(res_mach)
res_preds = predict(res_mach,Xnew)
# Plotting Results
scatter(X,y, label="Data",legend=:topleft,ms=2)
plot!(a:delta/100:b,f(a:delta/100:b), label="True Solution",ls=:dash)
plot!(X, mach.fitresult[:fit],label="Unconstrained")
scatter!(Xnew,preds,label="Unconstrained-Preds")
plot!(X, res_mach.fitresult[:fit],label="Lower/Upper/Increasing")
scatter!(Xnew,res_preds,label="Constrained-Preds")
- General usage examples/forrester.jl & examples/motor.jl.
- Shape restricted curves examples/constrained.jl, examples/mixing_constraints.jl & examples/bounds.jl.
- SmoothingSplines.jl: Follows the style of R's
smooth.spline
. Does not have an automatic selection of λ. Does not include shape constraints.
[1] I. J. Schoenberg, “Spline Functions and the Problem of Graduation”. 1964.
[2] G. Wahba, "Spline Models for Observational Data". SIAM, Jan. 1990.
[3] M. S. Andersen and T. Chen, “Smoothing Splines and Rank Structured Matrices: Revisiting the Spline Kernel”, SIAM Journal on Matrix Analysis and Applications, 2020.
[4] C. H. Reinsch "Smoothing by spline functions." Numerische mathematik 10.3 (1967): 177-183.
[5] H. C. Rytgaard, “Statistical models for robust spline smoothing”. MA thesis. University of Copenhagen, 2016.