Internal Functions

Main Structs

UnivariateFunctions.Undefined_FunctionType
Undefined_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

source
UnivariateFunctions.PE_FunctionType
PE_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.

source
UnivariateFunctions.Sum_Of_FunctionsType
Sum_Of_Functions <: UnivariateFunction

This function contants a vector of UnivariateFunctions. When evaluted it adds the evaluations of these functions and returns the sum.

source
UnivariateFunctions.Piecewise_FunctionType
Piecewise_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.

source

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.evaluateFunction
evaluate(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. DatePeriods are converted with the period_length function.

source
UnivariateFunctions.derivativeFunction
derivative(f::UnivariateFunction)

This calculates the derivative of the function and returns it as a UnivariateFunction.

Inputs

  • f - A UnivariateFunction.

Returns

  • A UnivariateFunction.
source
SchumakerSpline.evaluate_integralFunction
evaluate_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 - A UnivariateFunction.
  • left - A left limit (scalar)
  • right - A right limit (scalar)

Returns

  • A Real.
source
UnivariateFunctions.right_integralFunction
right_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 - A UnivariateFunction.
  • left - A left limit (scalar)

Returns

  • A UnivariateFunction.
source
UnivariateFunctions.left_integralFunction
left_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 - A UnivariateFunction.
  • right - A right limit (scalar)

Returns

  • A UnivariateFunction.
source

Interpolation

UnivariateFunctions.create_quadratic_splineFunction
create_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 - A Vector with the x coordinates
  • y - A Vector with the y coordinates
  • gradients - A Vector 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 - A Schumaker struct.

Returns

  • A Piecewise_Function containing the spline.
source
UnivariateFunctions.create_constant_interpolation_to_rightFunction
create_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 - A Vector with the x coordinates
  • y - A Vector with the y coordinates

Returns

  • A Piecewise_Function containing the interpolation function.
source
UnivariateFunctions.create_constant_interpolation_to_leftFunction
create_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 - A Vector with the x coordinates
  • y - A Vector with the y coordinates

Returns

  • A Piecewise_Function containing the interpolation function.
source
UnivariateFunctions.create_linear_interpolationFunction
create_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 - A Vector with the x coordinates
  • y - A Vector with the y coordinates

Returns

  • A Piecewise_Function containing the interpolation function.
source

Approximation

UnivariateFunctions.create_ols_approximationFunction
create_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 - A Vector with the y coordinates
  • x - A Vector with the x coordinates
  • base_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.
source
UnivariateFunctions.create_chebyshev_approximationFunction
create_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 approximation
  • nodes - The number of approximation nodes
  • degree - The degree of the Chebyshev polynomials.
  • left - The left limit of the approximation
  • right - The right limit of the approximation.

Returns

  • A Sum_Of_Functions containing the approximation function.
source

Internal Functions

UnivariateFunctions.change_base_of_PE_FunctionFunction
change_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 - A PE_Function.
  • new_base - The new base.

Returns

  • A PE_Function or a Sum_Of_Functions.
source
UnivariateFunctions.trim_piecewise_functionFunction
trim_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.
source
UnivariateFunctions.convert_to_linearly_rescale_inputsFunction
convert_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 - A UnivariateFunction.
  • alpha - The slope of the rescaling.
  • beta - The level of the rescaling.

Returns

  • A UnivariateFunction of the type that you input to the function.
source
UnivariateFunctions.get_chevyshevs_up_toFunction
get_chevyshevs_up_to(N::Integer, first_kind::Bool = true)

Get the first N chebyshev polynomials returned as a vector of UnivariateFunctions. 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 of UnivariateFunctions for each polynomial.
source

Date Conversions

UnivariateFunctions.years_betweenFunction
years_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 date
  • b - The start date.

Returns

  • A Real.
source
UnivariateFunctions.years_from_global_baseFunction
years_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.
source
UnivariateFunctions.period_lengthFunction
period_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.
source