irr_safe: Robust internal rate of return (adaptive bracketing)

View source: R/utils.R

irr_safeR Documentation

Robust internal rate of return (adaptive bracketing)

Description

Computes a real IRR from a vector of dated cash flows t = 0, \dots, T. The algorithm first searches for a root in an initial interval [lower, upper]. If this interval does not bracket a root (that is, if the net present value function does not change sign), the upper bound is expanded multiplicatively up to max_upper.

If the cash-flow series exhibits no sign change (all flows are >= 0 or all <= 0), or if no root can be bracketed after expansion, the function silently returns NA_real_ (optionally with a warning if warn = TRUE).

Usage

irr_safe(
  cf,
  lower = -0.9999,
  upper = 0.1,
  max_upper = 10000,
  tol = sqrt(.Machine$double.eps),
  warn = FALSE
)

Arguments

cf

Numeric. Vector of cash flows t = 0, \dots, T.

lower, upper

Initial search bounds for the IRR (decimal rates).

max_upper

Maximum upper bound when automatically expanding the bracketing interval.

tol

Numerical tolerance passed to uniroot.

warn

Logical. If TRUE, emits a warning when the IRR cannot be computed (no sign change or failure of bracketing).

Value

A numeric scalar (decimal rate) corresponding to the IRR, or NA_real_ if the IRR is not defined or could not be located numerically.

Examples

irr_safe(c(-100, 60, 60))   # IRR defined
irr_safe(c(-100, -20, -5))  # no sign change -> NA


cre.dcf documentation built on April 10, 2026, 5:08 p.m.