From 45599f8a10e57419895f13e7a694bbe427928a71 Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Sun, 30 Jul 2023 11:00:41 -0700 Subject: [PATCH 1/6] fv.annuity handles zero discount rate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Special case computed by applying L'Hôpital's rule as r->0. --- R/fv.annuity.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/fv.annuity.R b/R/fv.annuity.R index dec8fa7..265357b 100644 --- a/R/fv.annuity.R +++ b/R/fv.annuity.R @@ -14,8 +14,12 @@ fv.annuity <- function(r,n,pmt,type=0) { if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - fv = (pmt / r * ((1 + r)^n - 1))*(1+r)^type * (-1) - return(fv) + fv = if (isTRUE(all.equal(r, 0.0))){ + pmt * n * (-1) + }else{ + (pmt / r * ((1 + r)^n - 1))*(1+r)^type * (-1) + } + return(fv) } } From 76112e605ae4e6d2173d9f49da14960f1afe31af Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Fri, 4 Aug 2023 15:55:48 -0400 Subject: [PATCH 2/6] more functions corrected --- R/n.period.R | 8 ++++++-- R/pmt.R | 6 +++++- R/pv.annuity.R | 8 ++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/R/n.period.R b/R/n.period.R index 9dc9fea..f10cf70 100644 --- a/R/n.period.R +++ b/R/n.period.R @@ -18,8 +18,12 @@ n.period <- function(r,pv,fv,pmt,type=0){ if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - n <- log(-1 * (fv*r-pmt* (1+r)^type)/(pv*r+pmt* (1+r)^type))/log(1+r) - return(n) + n <- if (isTRUE(all.equal(r, 0.0))){ + -1 * (fv+pv) / pmt + }else{ + log(-1 * (fv*r-pmt* (1+r)^type)/(pv*r+pmt* (1+r)^type))/log(1+r) + } + return(n) } } diff --git a/R/pmt.R b/R/pmt.R index 68a04dd..d3ad5e9 100644 --- a/R/pmt.R +++ b/R/pmt.R @@ -19,7 +19,11 @@ pmt <- function(r,n,pv,fv,type=0){ if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - pmt <- (pv+fv/(1+r)^n)*r/(1-1/(1+r)^n) * (-1) * (1+r)^(-1 * type) + pmt <- if (isTRUE(all.equal(r, 0.0))){ + (pv+fv)/n * (-1) + }else{ + (pv+fv/(1+r)^n)*r/(1-1/(1+r)^n) * (-1) * (1+r)^(-1 * type) + } return(pmt) } } diff --git a/R/pv.annuity.R b/R/pv.annuity.R index c030e7a..8c6c87c 100644 --- a/R/pv.annuity.R +++ b/R/pv.annuity.R @@ -14,7 +14,11 @@ pv.annuity <- function(r, n, pmt, type=0) { if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - pv = (pmt / r * (1 - 1 / (1 + r)^n))*(1 + r)^type * (-1) - return(pv) + pv = if (isTRUE(all.equal(r, 0.0))){ + -n * pmt + }else{ + (pmt / r * (1 - 1 / (1 + r)^n))*(1 + r)^type * (-1) + } + return(pv) } } From 992c6107490401e42a3e08ec64b3b52178d7dc93 Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Fri, 4 Aug 2023 17:10:27 -0400 Subject: [PATCH 3/6] match style --- R/pv.annuity.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/pv.annuity.R b/R/pv.annuity.R index 8c6c87c..0521a65 100644 --- a/R/pv.annuity.R +++ b/R/pv.annuity.R @@ -15,7 +15,7 @@ pv.annuity <- function(r, n, pmt, type=0) { print("Error: type should be 0 or 1!") }else{ pv = if (isTRUE(all.equal(r, 0.0))){ - -n * pmt + n*pmt * (-1) }else{ (pmt / r * (1 - 1 / (1 + r)^n))*(1 + r)^type * (-1) } From 3aa536b45e56dafa458e91e46d38768bf7628abb Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Fri, 4 Aug 2023 18:46:47 -0400 Subject: [PATCH 4/6] undo previous commit --- R/pv.annuity.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/pv.annuity.R b/R/pv.annuity.R index 0521a65..4db1701 100644 --- a/R/pv.annuity.R +++ b/R/pv.annuity.R @@ -15,7 +15,7 @@ pv.annuity <- function(r, n, pmt, type=0) { print("Error: type should be 0 or 1!") }else{ pv = if (isTRUE(all.equal(r, 0.0))){ - n*pmt * (-1) + n * pmt * (-1) }else{ (pmt / r * (1 - 1 / (1 + r)^n))*(1 + r)^type * (-1) } From 57f91d355bb4dd3b9e49c2eee35860b23d5fcc7f Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Sat, 5 Aug 2023 13:56:34 -0400 Subject: [PATCH 5/6] tagged version 0.6.5 --- .gitignore | 1 + DESCRIPTION | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b9d6bd9..e0cdfde 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,4 @@ pip-log.txt #Mr Developer .mr.developer.cfg +.Rproj.user diff --git a/DESCRIPTION b/DESCRIPTION index 12a52bc..ca6a943 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: FinCal Title: Time Value of Money, Time Series Analysis and Computational Finance Description: Package for time value of money calculation, time series analysis and computational finance. -Version: 0.6.4 -Date: 2017-04-12 +Version: 0.6.5 +Date: 2023-08-05 Author: Felix Yanhui Fan Imports: ggplot2, From e077e5f095c2d79ad6f4159e2e23890c4e6db4b2 Mon Sep 17 00:00:00 2001 From: Steven Jenkins Date: Sun, 6 Aug 2023 12:38:50 -0400 Subject: [PATCH 6/6] minor tweak to all.equal() calls --- R/fv.annuity.R | 2 +- R/n.period.R | 2 +- R/pmt.R | 2 +- R/pv.annuity.R | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/fv.annuity.R b/R/fv.annuity.R index 265357b..f8080bb 100644 --- a/R/fv.annuity.R +++ b/R/fv.annuity.R @@ -14,7 +14,7 @@ fv.annuity <- function(r,n,pmt,type=0) { if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - fv = if (isTRUE(all.equal(r, 0.0))){ + fv = if (isTRUE(all.equal(0.0, r))){ pmt * n * (-1) }else{ (pmt / r * ((1 + r)^n - 1))*(1+r)^type * (-1) diff --git a/R/n.period.R b/R/n.period.R index f10cf70..badd7e4 100644 --- a/R/n.period.R +++ b/R/n.period.R @@ -18,7 +18,7 @@ n.period <- function(r,pv,fv,pmt,type=0){ if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - n <- if (isTRUE(all.equal(r, 0.0))){ + n <- if (isTRUE(all.equal(0.0, r))){ -1 * (fv+pv) / pmt }else{ log(-1 * (fv*r-pmt* (1+r)^type)/(pv*r+pmt* (1+r)^type))/log(1+r) diff --git a/R/pmt.R b/R/pmt.R index d3ad5e9..5a789f7 100644 --- a/R/pmt.R +++ b/R/pmt.R @@ -19,7 +19,7 @@ pmt <- function(r,n,pv,fv,type=0){ if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - pmt <- if (isTRUE(all.equal(r, 0.0))){ + pmt <- if (isTRUE(all.equal(0.0, r))){ (pv+fv)/n * (-1) }else{ (pv+fv/(1+r)^n)*r/(1-1/(1+r)^n) * (-1) * (1+r)^(-1 * type) diff --git a/R/pv.annuity.R b/R/pv.annuity.R index 4db1701..080f39b 100644 --- a/R/pv.annuity.R +++ b/R/pv.annuity.R @@ -14,7 +14,7 @@ pv.annuity <- function(r, n, pmt, type=0) { if(type != 0 && type !=1){ print("Error: type should be 0 or 1!") }else{ - pv = if (isTRUE(all.equal(r, 0.0))){ + pv = if (isTRUE(all.equal(0.0, r))){ n * pmt * (-1) }else{ (pmt / r * (1 - 1 / (1 + r)^n))*(1 + r)^type * (-1)