Is there any function/package that can compute year fraction (differences between two dates) with different day-counting convention, like yearfrac() in Matlab? I need to use Actual/365 convention.
Year fractions using Actual/365 convention in R
Asked Answered
rollYourOwn <- function(D, origin=as.Date("1970-01-01")) {
if (!inherits(D, "Date"))
D <- as.Date(D, origin=origin)
as.numeric(D - as.Date(format(D, "%Y-01-01"), origin=origin) + 1) / 365
}
rollYourOwn("2014-01-01")
# [1] 0.00273973
rollYourOwn(Sys.Date())
# [1] 0.742466
rollYourOwn("2014-12-31")
# [1] 1
What's the desired behaviour for leap years? For example,
rollYourOwn("2012-12-31")
is greater than one. Is that right? –
Battista @RichieCotton it depends on the market. This is an implementation of Actual/365 Fixed. Actual/365 would treat leap years differently. see support.treasuryview.com/kb/treasury/day-count-convention... edit: there's probably a more reliable source out there, I just picked the first thing I saw in google –
Agonized
© 2022 - 2024 — McMap. All rights reserved.
Rquantlib
has many day countersActual360
,Actual365
.... – Cusped