Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:43:59+00:00 2026-06-16T00:43:59+00:00

I have a big data set (around 200k rows) where each row is a

  • 0

I have a big data set (around 200k rows) where each row is a loan. I have the loan amount, the number of payments, and the loan payment.
I’m trying to get the loan rate.
R doesn’t have a function for calculating this (at least base R doesn’t have it, and I couldn’t find it).
It isn’t that hard to write both a npv and irr functions

Npv <- function(i, cf, t=seq(from=0,by=1,along.with=cf)) sum(cf/(1+i)^t)
Irr <- function(cf) { uniroot(npv, c(0,100000), cf=cf)$root }

And you can just do

rate = Irr(c(amt,rep(pmt,times=n)))

The problem is when you try to calculate the rate for a lot of payments. Because uniroot is not vectorized, and because rep takes a surprising amount of time, you end up with a slow calculation. You can make it faster if you do some math and figure out that you are looking for the roots of the following equation

zerome <- function(r) amt/pmt-(1-1/(1+r)^n)/r

and then use that as input for uniroot. This, in my pc, takes around 20 seconds to run for my 200k database.

The problem is that I’m trying to do some optimization, and this is a step of the optimization, so I’m trying to speed it up even more.

I’ve tried vectorization, but because uniroot is not vectorized, I can’t go further that way. Is there any root finding method that is vectorized?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:43:59+00:00Added an answer on June 16, 2026 at 12:43 am

    Instead of using a root finder, you could use a linear interpolator. You will have to create one interpolator for each value of n (the number of remaining payments). Each interpolator will map (1-1/(1+r)^n)/r to r. Of course you will have to build a grid fine enough so it will return r to an acceptable precision level. The nice thing with this approach is that linear interpolators are fast and vectorized: you can find the rates for all loans with the same number of remaining payments (n) in a single call to the corresponding interpolator.

    Now some code that proves it is a viable solution:

    First, we create interpolators, one for each possible value of n:

    n.max <- 360L  # 30 years
    
    one.interpolator <- function(n) {
        r <- seq(from = 0.0001, to = 0.1500, by = 0.0001)
        y <- (1-1/(1+r)^n)/r
        approxfun(y, r)
    }
    
    interpolators <- lapply(seq_len(n.max), one.interpolator)
    

    Note that I used a precision of 1/100 of a percent (1bp).

    Then we create some fake data:

    n.loans <- 200000L
    n     <- sample(n.max, n.loans, replace = TRUE)
    amt   <- 1000 * sample(100:500, n.loans, replace = TRUE)
    pmt   <- amt / (n * (1 - runif(n.loans)))
    loans <- data.frame(n, amt, pmt)
    

    Finally, we solve for r:

    library(plyr)
    system.time(ddply(loans, "n", transform, r = interpolators[[n[1]]](amt / pmt)))
    #    user  system elapsed 
    #   2.684   0.423   3.084
    

    It’s fast. Note that some of the output rates are NA but it is because my random inputs made no sense and would have returned rates outside of the [0 ~ 15%] grid I selected. Your real data won’t have that problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been running around here and there on NoSQL big data storage technologies.
I have been playing around with using graphs to analyze big data. Its been
I have an XML data set with 10K records, each containing a set of
I have to do extensive data manipulation on a big data set (using data.table,
I have a big data set into MySQL (users, companies, contacts)? about 1 million
I have a big data set from a questionary. Importing it from SPSS to
Suppose that you have a big Data Entry Web Application Like Microsoft CRM ,
I'm struggling with the following. If have a (big) data frame with the following:
I have very very big html page/data. I need to fetch data under h1
I have a big problem dealing with data I try to download in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.