Skip to content

Commit

Permalink
Adding support for DateTimes
Browse files Browse the repository at this point in the history
  • Loading branch information
s-baumann committed Oct 14, 2019
1 parent 6a69a78 commit d24f84c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/0_structs_and_generic_reversals.jl
Expand Up @@ -26,7 +26,7 @@ struct PE_Function{F<:Real,I<:Integer} <: UnivariateFunction
return new{promo_type,I}(promo_type(a_),promo_type(b_),promo_type(base_),I(d_))
end
end
function PE_Function(a_::R,b_::S,base_::Date,d_::I) where R<:Real where S<:Real where I<:Real
function PE_Function(a_::R,b_::S,base_::Union{Date,DateTime},d_::I) where R<:Real where S<:Real where I<:Real
new_base_ = years_from_global_base(base_)
return PE_Function(a_, b_, new_base_, d_)
end
Expand Down Expand Up @@ -69,14 +69,14 @@ struct Piecewise_Function <: UnivariateFunction
starts_without_pw_parts, functions_without_pw_parts = deal_with_piecewise_inputs(starts, functions)
new(starts_without_pw_parts, functions_without_pw_parts)
end
function Piecewise_Function(starts::Array{Date,1}, functions::Array)
function Piecewise_Function(starts::Union{Array{DateTime,1},Array{Date,1},Array{Union{DateTime,Date},1}}, functions::Array)
starts_ = years_from_global_base.(starts)
return Piecewise_Function(starts_, functions)
end
function Piecewise_Function(start::Real, func)
return Piecewise_Function([start], [func])
end
function Piecewise_Function(start::Date, func)
function Piecewise_Function(start::Union{Date,DateTime}, func)
start_float = years_from_global_base(start)
return Piecewise_Function(start_float, func)
end
Expand Down Expand Up @@ -226,7 +226,7 @@ function ^(number::Real, f::UnivariateFunction)
error("It is not possible yet to raise to the power of a UnivariateFunctions")
end

function evaluate(f::UnivariateFunction, d::Date)
function evaluate(f::UnivariateFunction, d::Union{Date,DateTime})
date_in_relation_to_global_base = years_from_global_base(d)
return evaluate(f, date_in_relation_to_global_base)
end
Expand Down
4 changes: 2 additions & 2 deletions src/1_undefined_function.jl
@@ -1,9 +1,9 @@
import Base.+, Base.-, Base./, Base.*, Base.^
import SchumakerSpline.evaluate
function evaluate(f::Undefined_Function, point::Union{Real,Date,DatePeriod})
function evaluate(f::Undefined_Function, point::Union{Real,Date,DateTime,DatePeriod})
missing
end
function (s::Undefined_Function)(x::Union{Real,Date,DatePeriod})
function (s::Undefined_Function)(x::Union{Real,Date,DateTime,DatePeriod})
return missing
end

Expand Down
2 changes: 1 addition & 1 deletion src/2_pe_functions.jl
Expand Up @@ -4,7 +4,7 @@ function evaluate(f::PE_Function, x::Real)
diff = x - f.base_
return f.a_ * exp(f.b_ * diff) * (diff)^f.d_
end
function (s::PE_Function)(x::Union{Real,Date,DatePeriod})
function (s::PE_Function)(x::Union{Real,Date,DateTime,DatePeriod})
return evaluate(s, x)
end

Expand Down
2 changes: 1 addition & 1 deletion src/3_sum_of_functions.jl
Expand Up @@ -7,7 +7,7 @@ function evaluate(sf::Sum_Of_Functions, point::Real)
end
return total
end
function (s::Sum_Of_Functions)(x::Union{Real,Date,DatePeriod})
function (s::Sum_Of_Functions)(x::Union{Real,Date,DateTime,DatePeriod})
return evaluate(s, x)
end

Expand Down
2 changes: 1 addition & 1 deletion src/4_piecewise_functions.jl
Expand Up @@ -4,7 +4,7 @@ function evaluate(f::Piecewise_Function, point::Real)
which_function = searchsortedlast(f.starts_, point)
return evaluate(f.functions_[which_function], point)
end
function (s::Piecewise_Function)(x::Union{Real,Date,DatePeriod})
function (s::Piecewise_Function)(x::Union{Real,Date,DateTime,DatePeriod})
return evaluate(s, x)
end

Expand Down
6 changes: 3 additions & 3 deletions src/5_calculus.jl
Expand Up @@ -7,7 +7,7 @@ function right_integral(f::UnivariateFunction, left::Real)
return indef_int - left_constant
end

function right_integral(f::UnivariateFunction, left::Date)
function right_integral(f::UnivariateFunction, left::Union{Date,DateTime})
left_float = years_from_global_base(left)
return right_integral(f, left_float)
end
Expand Down Expand Up @@ -46,7 +46,7 @@ function left_integral(f::UnivariateFunction, right::Real)
return right_constant - indef_int
end

function left_integral(f::UnivariateFunction, right::Date)
function left_integral(f::UnivariateFunction, right::Union{Date,DateTime})
right_float = years_from_global_base(right)
return left_integral(f, right_float)
end
Expand Down Expand Up @@ -86,7 +86,7 @@ function evaluate_integral(f::UnivariateFunction,left::Real, right::Real)
return (right_eval - left_eval)
end

function evaluate_integral(f::UnivariateFunction,left::Date, right::Date)
function evaluate_integral(f::UnivariateFunction,left::Union{Date,DateTime}, right::Union{Date,DateTime})
left_as_float = years_from_global_base(left)
right_as_float = years_from_global_base(right)
return evaluate_integral(f, left_as_float, right_as_float)
Expand Down
2 changes: 1 addition & 1 deletion src/6_splines_and_interpolation.jl
@@ -1,4 +1,4 @@
function create_quadratic_spline(x::Array{Date,1},y::Array{<:Real,1} ; gradients::Union{Missing,Array{<:Real,1}} = missing, extrapolation::Tuple{Schumaker_ExtrapolationSchemes,Schumaker_ExtrapolationSchemes} = (Curve,Curve), left_gradient::Union{Missing,Real} = missing, right_gradient::Union{Missing,Real} = missing)
function create_quadratic_spline(x::Union{Array{DateTime,1},Array{Date,1},Array{Union{Date,DateTime},1}},y::Array{<:Real,1} ; gradients::Union{Missing,Array{<:Real,1}} = missing, extrapolation::Tuple{Schumaker_ExtrapolationSchemes,Schumaker_ExtrapolationSchemes} = (Curve,Curve), left_gradient::Union{Missing,Real} = missing, right_gradient::Union{Missing,Real} = missing)
x_as_Floats = years_from_global_base.(x)
return create_quadratic_spline(x_as_Floats, y; gradients = gradients, extrapolation = extrapolation, left_gradient = left_gradient, right_gradient = right_gradient)
end
Expand Down
2 changes: 1 addition & 1 deletion src/7_regressions_and_approximation.jl
Expand Up @@ -28,7 +28,7 @@ function create_ols_approximation(y::Array{<:Real,1}, x::Array{<:Real,1}, base_x
return Sum_Of_Functions(func_array)
end

function create_ols_approximation(y::Array{<:Real,1}, x::Array{Date,1}, base_x::Date = global_base_date, degree::Integer = 1, intercept::Bool = true)
function create_ols_approximation(y::Array{<:Real,1}, x::Union{Array{DateTime,1},Array{Date,1},Array{Union{Date,DateTime},1}}, base_x::Union{Date,DateTime} = global_base_date, degree::Integer = 1, intercept::Bool = true)
base = years_from_global_base.(base_x)
xx = years_from_global_base.(x)
return create_ols_approximation(y, xx, base, degree, intercept)
Expand Down
5 changes: 3 additions & 2 deletions src/date_conversions.jl
Expand Up @@ -3,14 +3,15 @@ const days_per_year = 365.2422
const global_base_date = Date(2000,1,1)
const global_base_date_as_day = convert(Dates.Day, global_base_date)

function years_between(a::Date, b::Date)
function years_between(a::Union{DateTime,Date}, b::Union{DateTime,Date})
return (Dates.days(a) -Dates.days(b))/ days_per_year
end

function years_between(a::Dates.Day, b::Dates.Day)
return (convert(Int, a)-convert(Int, b))/ days_per_year
end

function years_from_global_base(a::Date)
function years_from_global_base(a::Union{DateTime,Date})
return years_between(a, global_base_date)
end

Expand Down
12 changes: 12 additions & 0 deletions test/2_date_tests.jl
Expand Up @@ -19,3 +19,15 @@ l_int = left_integral(pe_func, today)
(evaluate(l_int, date_in_2020) - evaluate_integral(pe_func,today,date_in_2020)) < tol
r_int = right_integral(pe_func, date_in_2020)
(evaluate(r_int, today) - evaluate_integral(pe_func,today,date_in_2020)) < tol

# With DateTimes
today_time = DateTime(2000,1,1, 10, 10, 1)
pe_func = PE_Function(1.0,2.0,today_time, 3)


# left and right integrals
later_today = DateTime(2000,1,1, 10, 14, 1)
l_int = left_integral(pe_func, today)
(evaluate(l_int, later_today) - evaluate_integral(pe_func,today,later_today)) < tol
r_int = right_integral(pe_func, later_today)
(evaluate(r_int, today) - evaluate_integral(pe_func,today,later_today)) < tol

0 comments on commit d24f84c

Please sign in to comment.