/* yldtomat.g Procedure to calculate the yield to maturity of one or more bonds. All inputs can be equally lengthed vectors, and/or scalers. The procedure will call sgrid1.g to conduct a univariate grid search to find the implicit interest rate which sets the present value of the cash streams equal to the current price of the bond Format: ytm=yldtomat(price,n,face,coupon) where: price is an nx1 vector or scaler for the current price of the bond(s) n is an nx1 vector or scaler for the number of periods (typically 6 month periods) until maturity face is an nx1 vector or scaler for the face value of the bond coupon is the percent coupon (as a percentage of face value) per period (typically half the coupon rate for bonds making semi-annual payments) note: - a coupon value greater than 1 will be converted to a percentage - coupon values less than 1 will be treated as the actual percent requires: yldtomat.g (this program) yldtomat.dec sgrid1.g sgrid1.dec be in your src sub-directory */ proc(1)=yldtomat(price,n,face,coupon); #include yldtomat.dec; #include sgrid1.dec; local maxrow,minrow,ytmvec,ytm,ofn,i; maxrow=maxc(rows(price)|rows(n)|rows(face)|rows(coupon)); minrow=minc(rows(price)|rows(n)|rows(face)|rows(coupon)); if maxrow>1 and minrow1; _coupon=_coupon./100; endif; {ytm,ofn}=sgrid1(0,1,20,5,&pv); ytmvec[i]=ytm; i=i+1; endo; retp(ytmvec); endp; proc(1)=pv(r); retp(-(_price-(_face/((1+r)^_n)+ sumc((_coupon.*_face)./((1+r).^seqa(1,1,_n))))).^2); endp;