Internal Functions
UnivariateFunctions.PE_Function
UnivariateFunctions.Piecewise_Function
UnivariateFunctions.Sum_Of_Functions
UnivariateFunctions.Undefined_Function
UnivariateFunctions.UnivariateFunction
SchumakerSpline.evaluate
SchumakerSpline.evaluate_integral
UnivariateFunctions.change_base_of_PE_Function
UnivariateFunctions.convert_to_linearly_rescale_inputs
UnivariateFunctions.create_chebyshev_approximation
UnivariateFunctions.create_constant_interpolation_to_left
UnivariateFunctions.create_constant_interpolation_to_right
UnivariateFunctions.create_linear_interpolation
UnivariateFunctions.create_ols_approximation
UnivariateFunctions.create_quadratic_spline
UnivariateFunctions.derivative
UnivariateFunctions.get_chevyshevs_up_to
UnivariateFunctions.indefinite_integral
UnivariateFunctions.left_integral
UnivariateFunctions.period_length
UnivariateFunctions.right_integral
UnivariateFunctions.trim_piecewise_function
UnivariateFunctions.years_between
UnivariateFunctions.years_from_global_base
Main Structs
UnivariateFunctions.UnivariateFunction
— TypeUnivariateFunction
An abstract type. The concrete structs that have been implemented are UndefinedFunction, PEFunction, SumOfFunctions, Piecewise_Function.
UnivariateFunctions.Undefined_Function
— TypeUndefined_Function <: UnivariateFunction
This function throws an error if you ever try to evaluate it. Think of it as doing the role of missing but for UnivariateFunctions
UnivariateFunctions.PE_Function
— TypePE_Function{F<:Real,I<:Integer} <: UnivariateFunction
This function has the functional form: a exp(b(x-base)) (x-base)^d Where a,b,base are floats and d is a positive integer. These four are the members of the struct.
UnivariateFunctions.Sum_Of_Functions
— TypeSum_Of_Functions <: UnivariateFunction
This function contants a vector of UnivariateFunctions. When evaluted it adds the evaluations of these functions and returns the sum.
UnivariateFunctions.Piecewise_Function
— TypePiecewise_Function <: UnivariateFunction
This function contants a vector of locations in the x space and a vector of UnivariateFunctions. When evaludated it uses these vectors as a lookup. It chooses the correct UnivariateFunction and evaluates it.
Function Evaluation and Calculus
Note that in addition to the below functions the following operators:
+
, -
, \
, *
, ^
have also been overloaded so that a function will be returned with the analytical sum, difference, product, quotient, power. The restrictions are that you cannot divide by a function (although you can divide by a scalar) and only positive integer powers can be taken.
SchumakerSpline.evaluate
— Functionevaluate(f::UnivariateFunction, r::Real)
evaluate(f::UnivariateFunction, d::Union{Date,DateTime})
evaluate(f::UnivariateFunction, x::DatePeriod)
This evaluates the function at the requested point. If a Date
, DateTime
is input then it is first converted to a scalar with the years_from_global_base
function. DatePeriod
s are converted with the period_length
function.
UnivariateFunctions.derivative
— Functionderivative(f::UnivariateFunction)
This calculates the derivative of the function and returns it as a UnivariateFunction
.
Inputs
f
- AUnivariateFunction
.
Returns
- A
UnivariateFunction
.
UnivariateFunctions.indefinite_integral
— Functionindefinite_integral(f::UnivariateFunction)
This calculates the indefinite integral of a UnivariateFunction
.
Inputs
f
- AUnivariateFunction
.
Returns
- A
UnivariateFunction
.
SchumakerSpline.evaluate_integral
— Functionevaluate_integral(f::UnivariateFunction,left::Real, right::Real)
evaluate_integral(f::UnivariateFunction,left::Union{Date,DateTime}, right::Union{Date,DateTime})
This calculates the integral of a function from a left limit to a right limit and returns a scalar.
If a Date
, DateTime
is input then it is first converted to a scalar with the years_from_global_base
function.
Inputs
f
- AUnivariateFunction
.left
- A left limit (scalar)right
- A right limit (scalar)
Returns
- A
Real
.
UnivariateFunctions.right_integral
— Functionright_integral(f::UnivariateFunction, left::Real)
right_integral(f::UnivariateFunction, left::Union{Date,DateTime})
This calculates the integral of a function from a left limit and returns it as a UnivariateFunction. So if you were to then evaluate this integral function at a point x then you would get the integral between left and x.
If a Date
, DateTime
is input then it is first converted to a scalar with the years_from_global_base
function.
Inputs
f
- AUnivariateFunction
.left
- A left limit (scalar)
Returns
- A
UnivariateFunction
.
UnivariateFunctions.left_integral
— Functionleft_integral(f::UnivariateFunction, right::Real)
left_integral(f::UnivariateFunction, right::Union{Date,DateTime})
This calculates the integral of a function from a right limit and returns it as a UnivariateFunction. So if you were to then evaluate this integral function at a point x then you would get the integral between right and x.
If a Date
, DateTime
is input then it is first converted to a scalar with the years_from_global_base
function.
Inputs
f
- AUnivariateFunction
.right
- A right limit (scalar)
Returns
- A
UnivariateFunction
.
Interpolation
UnivariateFunctions.create_quadratic_spline
— Functioncreate_quadratic_spline(x::Union{Vector{DateTime},Vector{Date},Vector{Union{Date,DateTime}}},y::Vector{<:Real} ; gradients::Union{Missing,Vector{<:Real}} = missing, extrapolation::Tuple{Schumaker_ExtrapolationSchemes,Schumaker_ExtrapolationSchemes} = (Curve,Curve),
left_gradient::Union{Missing,Real} = missing, right_gradient::Union{Missing,Real} = missing)
create_quadratic_spline(x::Vector{<:Real},y::Vector{<:Real} ; gradients::Union{Missing,Array{<:Real}} = missing, extrapolation::Tuple{Schumaker_ExtrapolationSchemes,Schumaker_ExtrapolationSchemes} = (Curve,Curve),
left_gradient::Union{Missing,Real} = missing, right_gradient::Union{Missing,Real} = missing)
create_quadratic_spline(x::Union{Vector{D},Vector{<:DatePeriod}},y::Vector{<:Real}; gradients::Union{Missing,Vector{<:Real}} = missing, extrapolation::Tuple{Schumaker_ExtrapolationSchemes,Schumaker_ExtrapolationSchemes} = (Curve,Curve),
left_gradient::Union{Missing,Real} = missing, right_gradient::Union{Missing,Real} = missing) where D<:DatePeriod
Makes a quadratic shape-preserving interpolation spline using the SchumakerSpline.jl package. This is returned as a Piecewise_Function
rather than as a Schumaker
struct.
Inputs
x
- AVector
with the x coordinatesy
- AVector
with the y coordinatesgradients
- AVector
with the gradiants at each x location. This is calculated if not provided.extrapolation
- A tuple of enum value describing how to extrapolate (on the left and right sides).left_gradient
- The gradiant to impose on the left edge (ie the first x coordinate).right_gradient
- The gradiant to impose on the right edge (ie the last x coordinate).
Returns
- A
Piecewise_Function
containing the spline.
create_quadratic_spline(schum::Schumaker)
This converts a spline represented by a SchumakerSpline.Schumaker
struct into the same spline but represented by a Piecewise_Function
.
Inputs
schum
- ASchumaker
struct.
Returns
- A
Piecewise_Function
containing the spline.
UnivariateFunctions.create_constant_interpolation_to_right
— Functioncreate_constant_interpolation_to_right(x::Vector{Date},y::Vector{<:Real})
create_constant_interpolation_to_right(x::Vector{<:Real},y::Vector{<:Real})
create_constant_interpolation_to_right(x::Union{Vector{D},Vector{<:DatePeriod}},y::Vector{<:Real}) where D<:DatePeriod
Makes a piecewise constant interpolation function. values from the left are copied to the right.
Inputs
x
- AVector
with the x coordinatesy
- AVector
with the y coordinates
Returns
- A
Piecewise_Function
containing the interpolation function.
UnivariateFunctions.create_constant_interpolation_to_left
— Functioncreate_constant_interpolation_to_left(x::Vector{Date},y::Vector{<:Real})
create_constant_interpolation_to_left(x::Vector{<:Real},y::Vector{<:Real})
create_constant_interpolation_to_left(x::Union{Vector{D},Vector{<:DatePeriod}},y::Vector{<:Real}) where D<:DatePeriod
Makes a piecewise constant interpolation function. values from the right are copied to the left.
Inputs
x
- AVector
with the x coordinatesy
- AVector
with the y coordinates
Returns
- A
Piecewise_Function
containing the interpolation function.
UnivariateFunctions.create_linear_interpolation
— Functioncreate_linear_interpolation(x::Vector{Date},y::Vector{<:Real})
create_linear_interpolation(x::Union{Vector{D},Vector{<:DatePeriod}},y::Vector{<:Real}) where D<:DatePeriod
create_linear_interpolation(x::Vector{R},y::Vector{<:Real}) where R<:Real
Makes a piecewise linear interpolation function. This is continuous.
Inputs
x
- AVector
with the x coordinatesy
- AVector
with the y coordinates
Returns
- A
Piecewise_Function
containing the interpolation function.
Approximation
UnivariateFunctions.create_ols_approximation
— Functioncreate_ols_approximation(y::Vector{<:Real}, x::Vector{<:Real}, base_x::Real = 0.0, degree::Integer = 1, intercept::Bool = true)
create_ols_approximation(y::Vector{<:Real}, x::Union{Vector{DateTime},Vector{Date},Vector{Union{Date,DateTime}}}, base_x::Union{Date,DateTime} = global_base_date, degree::Integer = 1, intercept::Bool = true)
An approximation function calculated via OLS.
Inputs
y
- AVector
with the y coordinatesx
- AVector
with the x coordinatesbase_x
- A real that offsets the x. So a coordinate with x value of 2.0 will be converted to 1.8 if base_x is 0.2.degree
- What the highest power of x should be. So if this is 3 then the equation will have x, x^2, x^3 as predictors.intercept
- Should there be an x intercept.
Returns
- A
Sum_Of_Functions
containing the approximation function.
UnivariateFunctions.create_chebyshev_approximation
— Functioncreate_chebyshev_approximation(func::Function, nodes::Integer, degree::Integer, left::Real, right::Real)
An function that will approximate another function via Chebyshev polynomials.
Inputs
func
- A function that you want to approximationnodes
- The number of approximation nodesdegree
- The degree of the Chebyshev polynomials.left
- The left limit of the approximationright
- The right limit of the approximation.
Returns
- A
Sum_Of_Functions
containing the approximation function.
Internal Functions
UnivariateFunctions.change_base_of_PE_Function
— Functionchange_base_of_PE_Function(f::PE_Function, new_base::Real)
This changes the base of a PE_Function
. So if the base was 2 then it can be converted to 3 with an additional constant term.
Inputs
f
- APE_Function
.new_base
- The new base.
Returns
- A
PE_Function
or aSum_Of_Functions
.
UnivariateFunctions.trim_piecewise_function
— Functiontrim_piecewise_function(func::Piecewise_Function, left_limit::Real, right_limit::Real)
This trims the end of a piecewise function. So if there is a piecewise function with support between -10,10 then you can trim it to only have support between -5 and 5. Then if it is evaluated outside -5 to 5 it will be undefined.
Inputs
func
- A Piecewise_Function.left_limit
- The left limit.right_limit
- The right limit.
Returns
- A
Piecewise_Function
.
UnivariateFunctions.convert_to_linearly_rescale_inputs
— Functionconvert_to_linearly_rescale_inputs(f::UnivariateFunction, alpha::Real, beta::Real)
This alters a function so that whenever we put in x it is like we put in alpha * x + beta
.
Inputs
f
- AUnivariateFunction
.alpha
- The slope of the rescaling.beta
- The level of the rescaling.
Returns
- A
UnivariateFunction
of the type that you input to the function.
UnivariateFunctions.get_chevyshevs_up_to
— Functionget_chevyshevs_up_to(N::Integer, first_kind::Bool = true)
Get the first N chebyshev polynomials returned as a vector of UnivariateFunction
s. The first 20 polynomials of each are precompiled into the binaries for speed. If you need more than that they will be calculated at runtime.
These can be from either the first kind or second kind polynomial sequence.
Inputs
N
- How many chebyshev polynomials do you want.first_kind
- A Bool. If true you get first kind polynomials. If false you get second kind.
Returns
- A
Vector
ofUnivariateFunction
s for each polynomial.
Date Conversions
UnivariateFunctions.years_between
— Functionyears_between(a::Union{DateTime,Date}, b::Union{DateTime,Date})
The number of years between two dates. This is returned as a scalar and assumes 365.2422 days per year.
Inputs
a
- The end dateb
- The start date.
Returns
- A
Real
.
UnivariateFunctions.years_from_global_base
— Functionyears_from_global_base(a::Union{DateTime,Date})
The number of years (calculated by the years_between
function) between a given date and the 1st of January 2000.
Inputs
date
- A date.
Returns
- A
Real
.
UnivariateFunctions.period_length
— Functionperiod_length(a::Dates.DatePeriod, base::Date = global_base_date)
Period length is designed to convert TimePeriod
objects to a float in a consistent way to years_from_global_base
. So effectively the years_between method is calculated with start and end dates being those at the start and end of a Dates.DatePeriod
. This is slightly complicated because a period like Month(3)
might have slightly different numbers of total days depending on when in the year it is. So a base date has to be input. The period is then measured starting from this base date.
Inputs
period
- A period.base
- A date from which the period will be measured from.
Returns
- A
Real
.