Dear Mata friends,
I have written this simple ready-to-execute code for computing the yield-to-maturity of a two-period coupon-paying bond:
However, this code is insufficient for my final purpose: I need to define the function s(.) dynamically for a varying number of cash-flows CF_1 ... CF_N.
Given my limited Mata knowledge, I think that the easiest solution is to write the respective formula as a string using Stata and then to evaluate this formula in the function s(ytm). The code would look like this:
Do you have any suggestions on how to evaluate the string formula "(100+23)/ytm^2 + 23/ytm - 100" using function s(ytm)?
Thank you,
David
I have written this simple ready-to-execute code for computing the yield-to-maturity of a two-period coupon-paying bond:
Code:
clear
input float(AMOUNTISSUED CF_1 CF_2) double YTM
100 23 22 .
end
mata
z=J(1,1,.)
st_view(z,., "YTM AMOUNTISSUED CF_1 CF_2")
function s(ytm,amountissued,cf_1,cf_2) {
return((amountissued+cf_2)/ytm^2+cf_1/ytm - amountissued)
}
for (i=1;i<=rows(z);i++) {
r=mm_root(YTM=.,&s(),epsilon(1),5-epsilon(1),1e-9,1000,z[i,2],z[i,3],z[i,4])
YTM-1
z[i,1]=YTM-1
}
end
However, this code is insufficient for my final purpose: I need to define the function s(.) dynamically for a varying number of cash-flows CF_1 ... CF_N.
Given my limited Mata knowledge, I think that the easiest solution is to write the respective formula as a string using Stata and then to evaluate this formula in the function s(ytm). The code would look like this:
Code:
clear input double YTM str20 formula . "(100+23)/ytm^2 + 23/ytm - 100" . "(100+20)/ytm^3 + 20/ytm^2 + 20/ytm- 100" *. "(100+20)/ytm^N + ... + 20/ytm^2 + 20/ytm- 100" end mata z=J(1,1,.) st_view(z,., "YTM") function s(ytm) { return(??????????) } for (i=1;i<=rows(z);i++) { r=mm_root(YTM=.,&s(),epsilon(1),5-epsilon(1),1e-9,1000) YTM-1 z[i,1]=YTM-1 } end
Do you have any suggestions on how to evaluate the string formula "(100+23)/ytm^2 + 23/ytm - 100" using function s(ytm)?
Thank you,
David
Comment