Year fractions using Actual/365 convention in R
Asked Answered
M

1

6

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.

Majunga answered 28/9, 2014 at 9:52 Comment(1)
Rquantlib has many day counters Actual360,Actual365....Cusped
R
4
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
Rosiorosita answered 28/9, 2014 at 10:2 Comment(2)
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 googleAgonized

© 2022 - 2024 — McMap. All rights reserved.