# commons-user mailing list archives

##### Site index · List index
Message view
Top
From "David Hoffer" <david.hof...@issinc.com>
Subject RE: commons-math usage to calculate APR?
Date Sun, 17 Aug 2008 00:10:34 GMT
```My mistake; I had the formula wrong, the part I thought was in the
denominator actually divides one of the other factors.  The right formula
is:

(x * Math.pow(1 + x, numMonths))/((Math.pow(1 + x, numMonths) - 1)) -
(pPrime / loanAmount);

Thanks for your help, it works great!

-Dave

-----Original Message-----
From: Luc Maisonobe [mailto:Luc.Maisonobe@free.fr]
Sent: Saturday, August 16, 2008 5:32 PM
To: Commons Users List
Subject: Re: commons-math usage to calculate APR?

David Hoffer a écrit :
> Well I am getting close with your suggestions.
>
> It seems to be calculating values just a little low.  Perhaps my formula
is
> not quite right.  Just to be clear, why did you drop the denominator?

Only for the sake of solving the equation.
Solving A/B = 0 or solving A = 0 leads to the same result.

>
> As an example of current results, if the output should be 5% and then get
> higher as loanExpenses goes up, it is calculating about 4.7% and then gets
> higher as loanExpenses go up (but it stays low).

You can check that the computed value really solves the equation by
printing f(apr).

If it is not almost zero, it means the convergence settings are too
loose. Look at the convergence threshold, or max number of iterations.
The javadoc for BrentSolver explains the different settings you can change.

If the result is almost zero, it means the initial equation does not
compute what you want.

Luc

>
> -Dave
>
> -----Original Message-----
> From: Luc Maisonobe [mailto:Luc.Maisonobe@free.fr]
> Sent: Saturday, August 16, 2008 4:14 PM
> To: Commons Users List
> Subject: Re: commons-math usage to calculate APR?
>
> David Hoffer a écrit :
>> Hum, simple I'm sure but perhaps calculating APR is not quite that
simple;
>> you have to factor in loan expenses (that's why APR is not the same as
>> interest rate).
>>
>> I think it is something like:
>>
>> P' = (P+E)I(1+i)^n/(1+i)^n
>>
>> Then you solve for APR with the non-linear function which is something
> like:
>> ((a(1+a)^n)-(P'/P))/((1+a)^n-1) = 0
>>
>> Where a=ARR/12
>
> In order to solve this, you can first drop the denominator (which is non
> null if a is non null). You end up with:
>
>   a(1+a)^n - P'/P = 0
>
>
> Solving this using for any commons-math solver (for example BrentSolver)
> could probably be done as follows:
>
>   final double p      = ...;
>   final double pPrime = ...;
>   final int    n      = ...;
>   UnivariateRealFunction equation = new UnivariateRealFunction() {
>       public double value(double x) {
>          return x * Math.pow(1 + x, n) - pPrime / p;
>       }
>   };
>   UnivariateRealSolver solver = new BrentSolver(equation);
>   double apr = solver.solve(0.001, 0.999);
>
> Using final variables for p, pPrime and n allow to refer to them
> directly in the anonymous class defined by the new
> UnivariateRealFunction call. If you prefer a more elegant solution, you
> can create a dedicated class implementing the UnivariateRealFunction and
>  that would use private fields to hold these values.
>
>
> Luc
>
>> Calculating P' is straightforward.  Solving for a is where I'm not clear,
>> plus I'm not certain of the math above.
>>
>> -Dave
>>
>> -----Original Message-----
>> From: Bear Giles [mailto:bgiles@coyotesong.com]
>> Sent: Saturday, August 16, 2008 3:40 PM
>> To: Commons Users List
>> Subject: Re: commons-math usage to calculate APR?
>>
>> There are closed-form expressions for that, although I don't have them
>> off the top of my head.  Something like
>>
>>    m = P / (1 - i)^n
>>
>> or maybe
>>
>>   m = P / ((1 + i)^n - 1)
>>
>> where m is monthly payment, P is principal, i is monthly interest, n is
>> number of months.  It's definitely not very complex for a simple
>> fixed-term, fixed-rate loan.
>>
>> Bear
>>
>> David Hoffer wrote:
>>> My usage is for USA only at this point.
>>>
>>> I have inputs of loanAmount, monthlyInterestRate, numberMonths,
loanFees;
>> so
>>> this would be for fixed monthly payments.
>>>
>>> I understand that commons-math doesn't have specific finance methods
>> however
>>> I think the APR formula is a non-linear equation that I was hoping
>>> commons-math could help solve.
>>>
>>> -Dave
>>>
>>> -----Original Message-----
>>> From: Luc Maisonobe [mailto:Luc.Maisonobe@free.fr]
>>> Sent: Saturday, August 16, 2008 2:45 PM
>>> To: Commons Users List
>>> Subject: Re: commons-math usage to calculate APR?
>>>
>>> David Hoffer a écrit :
>>>
>>>> Can anyone point me to an example of how to use commons-math to
> calculate
>>>> APR (Annual Percentage Rate)?
>>>>
>>> There are no specific finance related algorithm in commons-math.
>>>
>>>
>>>> I think this is solvable using either the NewtonSolver or
> BisectionSolver
>>>> but I am not sure how to accomplish this.  Perhaps a different way is
>>>> better.
>>>>
>>> I think so, but it depends on what you really need. APR computation
>>> seems to be slightly different depending on local regulations (for
>>> example USA or EU). It also depends on the assumptions you do and the
>>> input data. For example you can compute it one way for fixed monthly
>>> payments and another way for varying monthly payments.
>>>
>>>
>>>> Any help would be greatly appreciated.
>>>>
>>> Could you explain your needs more precisely ?
>>>
>>> Luc
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org