Internal Functions
SchumakerSpline.SchumakerSchumakerSpline.Schumaker2dSchumakerSpline.Schumaker_ExtrapolationSchemesSchumakerSpline.evaluateSchumakerSpline.evaluate_integralSchumakerSpline.extrapolateSchumakerSpline.find_derivative_splineSchumakerSpline.find_optimaSchumakerSpline.find_rootsSchumakerSpline.getCoefficientMatrixSchumakerSpline.get_crossover_in_intervalSchumakerSpline.get_intersection_pointsSchumakerSpline.imputeGradientsSchumakerSpline.plotSchumakerSpline.quadratic_formula_rootsSchumakerSpline.reshape_valuesSchumakerSpline.schumakerIndInterval!SchumakerSpline.splice_splinesSchumakerSpline.test_if_intercept_in_interval
Main Struct
SchumakerSpline.Schumaker — Type
Schumaker(x::Array{T,1},y::Array{T,1} ; gradients::Union{Missing,Array{T,1}} = missing, extrapolation::Schumaker_ExtrapolationSchemes = Curve,
left_gradient::Union{Missing,T} = missing, right_gradient::Union{Missing,T} = missing)
Schumaker(x::Array{Int,1},y::Array{T,1} ; gradients::Union{Missing,Array{T,1}} = missing, extrapolation::Schumaker_ExtrapolationSchemes = Curve,
left_gradient::Union{Missing,T} = missing, right_gradient::Union{Missing,T} = missing)
Schumaker(x::Array{Date,1},y::Array{T,1} ; gradients::Union{Missing,Array{T,1}} = missing, extrapolation::Schumaker_ExtrapolationSchemes = Curve,
left_gradient::Union{Missing,T} = missing, right_gradient::Union{Missing,T} = missing)Creates a Schumaker spline.
Inputs
x- A vector of x coordinates.y- A vector of y coordinates.extrapolation- This should beCurve,LinearorConstantspecifying how to interpolate outside of the sample domain.gradients- A vector of gradients at each point. If not supplied these are imputed from x and y.left_gradient- The gradient at the lowest value of x in the domain. This will override the gradient imputed or submitted in the gradients optional argument (if it is submitted there)right_gradient- The gradient at the highest value of x in the domain. This will override the gradient imputed or submitted in the gradients optional argument (if it is submitted there)
Returns
- A
Schumakerobject which contains the spline. This object can then be evaluated with evaluate or evaluate_integral.
SchumakerSpline.evaluate — Function
Evaluates the spline at a point. The point can be specified as a Real number (Int, Float, etc) or a Date. Derivatives can also be taken.
Inputs
spline- ASchumakertype splinePointToExamine- The point at which to evaluate the integralderivative- The derivative being sought. This should be 0 to just evaluate the spline, 1 for the first derivative or 2 for a second derivative.
Higher derivatives are all zero (because it is a quadratic spline). Negative values do not give integrals. Use evaluate_integral instead.
Returns
- A value of the spline or appropriate derivative in the same format as specified in the spline.
evaluate(spline::Schumaker2d, p1::Real, p2::Real)Inputs
spline- A Schumaker2dp1- The coordinate in the first dimension.p2- The coordinate in the second dimension.
Returns
- A scalar
SchumakerSpline.evaluate_integral — Function
Estimates the integral of the spline between lhs and rhs. These end points can be input as Reals or Dates.
Inputs
spline- A Schumaker type splinelhs- The left hand limit of the integralrhs- The right hand limit of the integral
Returns
- A
Float64value of the integral.
Extrapolation Schemes
SchumakerSpline.Schumaker_ExtrapolationSchemes — Type
This creates an enum which details how extrapolation from the interpolation domain should be done. The possible enum values are:
Curve- Curve extrapolation extends out the quadratic form at the edges. This can lead to a nonmonotonic result (as the curve can eventually change direction)Linear- Linear extrapolation extends out the gradient from at the edges. This will always lead to a monotonic result.Constant- Linear extrapolation extends out the value from at the edges. This leads to flat values being extended out.
Working with Splines
SchumakerSpline.find_derivative_spline — Function
findderivativespline(spline::Schumaker) Returns a SchumakerSpline that is the derivative of the input spline
SchumakerSpline.find_roots — Function
find_roots(spline::Schumaker{T}; root_value::Real = 0.0, interval::Tuple{<:Real,<:Real} = (spline.IntStarts_[1], spline.IntStarts_[length(spline.IntStarts_)])) where T<:RealFinds roots - This is handy because in many applications schumaker splines are monotonic and globally concave/convex and so it is easy to find roots. Here root_value can be set to get all points at which the function is equal to the root value. For instance if you want to find all points at which the spline has a value of 1.0.
Inputs
spline- The spline you want to find the roots for.root_value- What level counts as a root.interval- What interval to explore for roots.
Returns
- A
NamedTupledescribing all roots found together with the derivatives and second derivatives at that point.
SchumakerSpline.find_optima — Function
find_optima(spline::Schumaker)Finds optima - This is handy because in many applications schumaker splines are monotonic and globally concave/convex and so it is easy to find optima.
Inputs
spline- The spline you want to find optima for.interval- The interval over which you want to look for optima.
Returns
- A NamedTuple containing the optima and the types of the optima (:Maximum or :Minimum)
SchumakerSpline.get_crossover_in_interval — Function
get_crossover_in_interval(s1::Schumaker{T}, s2::Schumaker{R}, interval::Tuple{U,U}) where T<:Real where R<:Real where U<:RealFinds the point at which two schumaker splines cross over each other within a single interval.
Inputs
s1- The first splines2- The second splineinterval- The interval you want to examine for crossovers.
Returns
- A
Vectordescribing crossover points.
SchumakerSpline.get_intersection_points — Function
get_intersection_points(s1::Schumaker{T}, s2::Schumaker{R}) where T<:Real where R<:RealThis funds the coordinates of the point at which spline s1 intercepts spline s2.
Inputs
s1- The first splines2- The second spline
Returns
- Locations of any crossover points.
SchumakerSpline.reshape_values — Function
reshape_values(xvals::Vector{<:Real}, yvals::Vector{<:Real}; increasing::Bool = true,
concave::Bool = true, shape_map::Function = shape_map)This reshapes a vector of yvalues. For instance if we are doing fixed point acceleration that should result in a monotonic concave function (ie the consumption smoothing problem from the documentation examples) then we may end up with occasional non monotonic/concave values due to a dodgy optimiser or some other numerical issue. So we can use reshape_values to adjust the values that cannot be true.
Inputs
xvals- A vector of x coordinates.yvals- A vector of y coordinatesincreasing- Should the y values be increasing. If false then they must be decreasingconcave- Should the y values be concave. If false then they must be convexshape_map- A function used to adjust values to be increasing-concave (or whatever settings)
Returns
- An updated vector of y values.
SchumakerSpline.splice_splines — Function
splice_splines(left_spline::Schumaker, right_spline::Schumaker, splice_point::Real)This puts two splines together. Making a new spline. Note that the stitched together spline is not guaranteed to be continuous or shape preserving anymore.
Inputs
left_spline- The spline to use on the left.right_spline- The spline to use on the right.splice_point- The x coordinate to stitch at.
Returns
- A Schumaker struct
Two dimensional splines
SchumakerSpline.Schumaker2d — Type
This uses a combination of Schumaker Splines to cover a 2 dimensional space. We have a grid of splines. We first evaluate in one dimension (which leads us to a point between two adjacent splines). Then we evaluate each of the two splines and interpolate.
Members
IntStarts_- A vector with the coordinates of each schumaker spline.schumakers- A vector of schumaker splines.
Plotting of splines
SchumakerSpline.plot — Function
plot(s1::Schumaker, interval::Tuple{R,R}; derivs=false, grid_len=200, label="Spline",
deriv_label="1st derivative", deriv2_label="2nd derivative",
width_in_cm=20, height_in_cm=12) where R<:Real
plot(s1::Schumaker, grid::AbstractArray{R,1}; derivs=false, label="Spline",
deriv_label="1st derivative", deriv2_label="2nd derivative",
width_in_cm=20, height_in_cm=12) where R<:RealInputs
s1- The Schumaker spline to chartinterval- The interval over which to chart it.grid_len- The number of grid points to be used in plotting.grid- The grid. If used this is instead of theintervalandgrid_lenderivs- Should the derivative splines also be plottedlabel- Label for the spline curvederiv_label- Label for the first derivative curvederiv2_label- Label for the second derivative curvewidth_in_cm- Width of the plot in centimeters (default 20)height_in_cm- Height of the plot in centimeters (default 12)
Returns
- A VegaLite plot specification
plot(ss::Vector{<:Schumaker}, interval::Tuple{R,R}; derivs=false, grid_len=200,
labels=missing, width_in_cm=20, height_in_cm=12) where R<:Real
plot(ss::Vector{<:Schumaker}, grid::AbstractArray{R,1}; derivs=false,
labels=missing, width_in_cm=20, height_in_cm=12) where R<:RealInputs
ss- a vector of Schumaker splines to chartinterval- The interval over which to chart it.grid_len- The number of grid points to be used in plotting.grid- The grid. If used this is instead of theintervalandgrid_lenderivs- Should the derivative splines also be plottedlabels- Labels for the spline curves. Defaults to "Spline 1", "Spline 2", etc.width_in_cm- Width of the plot in centimeters (default 20)height_in_cm- Height of the plot in centimeters (default 12)
Returns
- A VegaLite plot specification
Internal Functions
SchumakerSpline.schumakerIndInterval! — Function
Writes 1 or 2 rows into buffer starting at row row. Returns the number of rows written (1 or 2). Buffer columns are [x_start, a, b, c].
SchumakerSpline.imputeGradients — Function
imputeGradients(x::Vector{T}, y::Vector{T})Imputes gradients based on a vector of x and y coordinates.
SchumakerSpline.quadratic_formula_roots — Function
quadratic_formula_roots(a::Real,b::Real,c::Real)A basic application of the textbook quadratic formula.
Inputs
a- The quadratic termb- The linear termc- The constant
Returns
- A vector with the roots.
SchumakerSpline.test_if_intercept_in_interval — Function
testifinterceptininterval(a1::Real,b1::Real,c1::Real,c2::Real,interval_width::Real) This tests if a spline could have passed over zero in a certain interval. The a1,b1,c1 are the coefficients of the spline. The two xs are for the left and right and c2 is the right hand level. Note that this function will not detect zeros that are precisely on the endpoints.
SchumakerSpline.getCoefficientMatrix — Function
Calls SchumakerIndInterval many times to get full set of spline intervals and coefficients. Then calls extrapolation for out of sample behaviour
Inputs
gradients- A vector of gradients at each pointx- A vector ofxcoordinatesy- A vector ofycoordinatesextrapolation- A string in (Curve, Linear or Constant) that gives behaviour outside of interpolation range.
Returns
- A vector of interval starts
- A vector of interval ends
- A matrix of all coefficients
SchumakerSpline.extrapolate — Function
Adds a row on top and bottom of coefficient matrix to give out of sample prediction.
Inputs
fullMatrix- output fromGetCoefficientMatrixfirst few linesextrapolation- A tuple with two enums in (Curve, Linear, Constant) that gives behaviour outside of interpolation range.x- A vector of x coordinatesy- A vector of y coordinates
Returns
- A new version of fullMatrix with out of sample prediction built into it.