Internal Functions
SchumakerSpline.Schumaker
SchumakerSpline.Schumaker2d
SchumakerSpline.Schumaker_ExtrapolationSchemes
RecipesBase.plot
SchumakerSpline.evaluate
SchumakerSpline.evaluate_integral
SchumakerSpline.extrapolate
SchumakerSpline.find_derivative_spline
SchumakerSpline.find_optima
SchumakerSpline.find_roots
SchumakerSpline.getCoefficientMatrix
SchumakerSpline.get_crossover_in_interval
SchumakerSpline.get_intersection_points
SchumakerSpline.imputeGradients
SchumakerSpline.quadratic_formula_roots
SchumakerSpline.reshape_values
SchumakerSpline.schumakerIndInterval
SchumakerSpline.splice_splines
SchumakerSpline.test_if_intercept_in_interval
Main Struct
SchumakerSpline.Schumaker
— TypeSchumaker(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
,Linear
orConstant
specifying 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
Schumaker
object which contains the spline. This object can then be evaluated with evaluate or evaluate_integral.
SchumakerSpline.evaluate
— FunctionEvaluates 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
- ASchumaker
type 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
— FunctionEstimates 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
Float64
value of the integral.
Extrapolation Schemes
SchumakerSpline.Schumaker_ExtrapolationSchemes
— TypeThis 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
— Functionfindderivativespline(spline::Schumaker) Returns a SchumakerSpline that is the derivative of the input spline
SchumakerSpline.find_roots
— Functionfind_roots(spline::Schumaker{T}; root_value::Real = 0.0, interval::Tuple{<:Real,<:Real} = (spline.IntStarts_[1], spline.IntStarts_[length(spline.IntStarts_)])) where T<:Real
Finds 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
NamedTuple
describing all roots found together with the derivatives and second derivatives at that point.
SchumakerSpline.find_optima
— Functionfind_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
— Functionget_crossover_in_interval(s1::Schumaker{T}, s2::Schumaker{R}, interval::Tuple{U,U}) where T<:Real where R<:Real where U<:Real
Finds 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
Vector
describing crossover points.
SchumakerSpline.get_intersection_points
— Functionget_intersection_points(s1::Schumaker{T}, s2::Schumaker{R}) where T<:Real where R<:Real
This 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
— Functionreshape_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
— Functionsplice_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
— TypeThis 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
RecipesBase.plot
— Functionplot(s1::Schumaker, interval::Tuple{R,R} = (s1.IntStarts_[1], s1.IntStarts_[length(s1.IntStarts_)]); derivs::Bool = false, grid_len::Integer = 200, plot_options::NamedTuple = (label = "Spline",), deriv_plot_options::NamedTuple = (label = "Spline - 1st deriv",),
deriv2_plot_options::NamedTuple = (label = "Spline - 2nd deriv",), plt = missing) where R<:Real
plot(s1::Schumaker, grid::AbstractArray{R,1}; derivs::Bool = false, plot_options::NamedTuple = (label = "Spline",), deriv_plot_options::NamedTuple = (label = "Spline - 1st deriv",),
deriv2_plot_options::NamedTuple = (label = "Spline - 2nd deriv",), plt = missing) where R<:Real
Inputs
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 theinterval
andgrid_len
derivs
- Should the derivative splines also be plottedplot_options
- The options to use in plottingderiv_plot_options
- The options to use in the first derivative plot.deriv2_plot_options
- The options to use in the second derivative plot.plt
- A plot object. Feed this in if you want to add to a plot (rather than make a new one.)
Returns
- A plot
plot(ss::Vector{Schumaker}, interval::Tuple{R,R} = (ss[1].IntStarts_[1], ss[1].IntStarts_[length(ss[1].IntStarts_)]); derivs::Bool = false, grid_len::Integer = 200, plot_options::Union{AbstractArray{Tuple,1},Missing} = missing,
deriv_plot_options::Union{AbstractArray{Tuple,1},Missing} = missing, deriv2_plot_options::Union{AbstractArray{Tuple,1},Missing} = missing, plt = missing) where R<:Real
plot(ss::Vector{Schumaker}, grid::Vector{R}; derivs::Bool = false, plot_options::Union{AbstractArray{Tuple,1},Missing} = missing, deriv_plot_options::Union{AbstractArray{Tuple,1},Missing} = missing,
deriv2_plot_options::Union{AbstractArray{Tuple,1},Missing} = missing, plt = missing) where R<:Real
Inputs
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 theinterval
andgrid_len
derivs
- Should the derivative splines also be plottedplot_options
- The options to use in plottingderiv_plot_options
- The options to use in the first derivative plot.deriv2_plot_options
- The options to use in the second derivative plot.plt
- A plot object. Feed this in if you want to add to a plot (rather than make a new one.)
Returns
- A plot
Internal Functions
SchumakerSpline.schumakerIndInterval
— FunctionSplits an interval into 2 subintervals and creates the quadratic coefficients
Inputs
gradients
- A 2 entry vector with gradients at either end of the intervaly
- A 2 entry vector with y values at either end of the intervalx
- A 2 entry vector with x values at either end of the interval
Returns
- A 2 x 4 matrix. The first column is the x values of start of the two subintervals. The last 3 columns are quadratic coefficients in two subintervals.
SchumakerSpline.imputeGradients
— FunctionimputeGradients(x::Vector{T}, y::Vector{T})
Imputes gradients based on a vector of x and y coordinates.
SchumakerSpline.quadratic_formula_roots
— Functionquadratic_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
— Functiontestifinterceptininterval(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
— FunctionCalls 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 ofx
coordinatesy
- A vector ofy
coordinatesextrapolation
- 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
— FunctionAdds a row on top and bottom of coefficient matrix to give out of sample prediction.
Inputs
fullMatrix
- output fromGetCoefficientMatrix
first 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.