View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joeu2004 joeu2004 is offline
external usenet poster
 
Posts: 2,059
Default HELP NEEDED - Iterative process in a VBA function

On Mar 13, 2:19 am, Paul S wrote:
Thanks for your response, in the meantime I have received
some other help. The function now looks like this and it
works!


I can confirm that it converges on a result. It is unclear to me why,
and I cannot say whether or not the result is correct (the definition
of "it works"). But that is because I am not familiar with the
financial theory here.

However, I do believe there might be instances where the loop does not
terminate. That is because "loan_amt < settlement_amount + fpa_cost"
might never be true, even though it should be mathematically, due to
anomalies of binary floating-point arithmetic, which is the internal
representation of type Double.

(Admittedly, it works for the several example parameters that I
tested.)

I assume that at least "coupon" and risk_free_rate are non-integers,
and I suspect that "fraction" is also a non-integer. In any case, it
appears that fpa_cost might become a non-integer.

With type Double (binary floating-point), most non-integers (and most
integers 2^53) cannot be represented exactly. Thus, arithmetic
often results in unexpected anomalies. For example, 10.1 - 10 < 0.1
(!). Likewise, adding 0.1 repeatedly starting with 0 will never
exactly equal 10.1 (!).

Therefore, it would be prudent to implement the loop termination
condition by one of the following options:

Do While Abs(settlement_amount + fpa_cost - loan_amt) = 0.0000005

Do While Round(settlement_amount + fpa_cost - loan_amt, 6) < 0

The choice of 0.0000005 and 6 is arbitrary. I chose them based on
rounding precision used to compute FPA_test.

Aside.... It is not necessary to compute FPA_test within the loop.
There is no advantage to computing it within the loop as it is written.