Dear Statalist,
I have been trying to solve for r in the function described below. I cannot figure out why I keep getting "." for an answer. The helpfile for mm_root says that
x will be set to missing if
f evaluates to missing at some point in the algorithm.
In my case x is r, so maybe this is related to my problem. I have been playing around with the boundaries trying to avoid r=0 since my function is undefined at that point. I am focusing on boundaries in the negative side because this chunk of code is to solve for interest rates in loans with negative net current value. I know there is an answer to the sample problem below because I found it using excel. Any help would be greatly appreciated!
Code:
local coupon = 100
display `coupon'
local periods = 24
display `periods'
local amount = 5000
display `amount'
*** Build function
mata: mata clear
mata
function myfunc(r, c, p, a) {
t1 = (c/r)
t2 = -(1/((1 + r)^(p)))*t1
t3 = -a
return(t1 + t2 + t3)
}
c = `coupon'
p = `periods'
a = `amount'
mm_root(r=., &myfunc(), -100, smallestdouble(), 1e-9, 1000, c, p, a)
r
end
About the code: my plan is to call this simple function from another do file to fill in the interest rates of each loan (rows) in a new data file. The coupon, period, and amount in the code are for a sample loan with slightly negative net current value. I plan on replicating this chunk of code with positive boundaries for loans with positive net current value. Both do files will be called from within a loop in the file which I use to construct the interest rate data.
Alvaro
0 Response to Cannot get mm_root to solve an equation for which excel shows there is an answer
Post a Comment