In the irr(cf)
function, the first if
and elseif
statements are intended to exclude the cases where there will be no solution. Specifically, if the first element in cf
is zero and there are any other non-zero elements, then it should return Inf
. If the first element in cf
is non-zero but the remainder are zeros then it should return NaN
.
Now, if these two conditional statements are not true then the optimizer is run on npv
to find irr
. The npv
will use all elements of cf
including zeros (of course excluding the cases that would fall in the first two conditional statements), e.g.
julia> irr([-100,0,10,10,0,0,110])
0.049594202242656926
I should have added an if
statement to return NaN
or a numerical error in this case.
The optimizer may fail to converge if the bounds are set too wide or give a negative result when a positive result is possible. This is the reason I am changing the bounds.
This is the bit I am struggling with. I can define the bounds as below, but I do not understand how to iterate over them until the convergence criteria is satisfied, probably a while
loop is required. How should I use the loop?
lb= [0.0,1.0,-1.0,-1.0e8]
up=[1.0,1.0e8,0.0,0.0]
bounds = hcat(lb,up)