Dear R helpers
 
I have following csv file which is an input
 
id       par_value    coupon_rate     frequency_coupon   tenure    ytm
 
1            1000             10                      1                     
5          12
 
# Here frequency_coupon is coded s.t. 0 means Daily compounding, 1 means 
monthly compouding, 2 means Quarterly, 3 means Half yearly and 4 means 
only once. Thus in the case the frequency_coupon = 1 means, total number of 
times compounding is done = 12.   
 
 
My R Code for calcualting Macaulay Duration is as follows -
 
## INPUT
 
ONS              = read.csv('instrument details..csv')
par_value       = ONS$par_value
coupon          = ONS$coupon_rate*par_value/100
freq_coupon   = ONS$frequency_copoun
tenure           = ONS$tenure
ytm              = ONS$ytm
 
# 
_________________________________________________________________________________________________
 
## COMPUTATIONS

macaulay_duration =   NULL
modified_duration   =   NULL 
freq_coupon_new   =   NULL
 
if(freq_coupon <= 0)
{
    freq_coupon_new = 365
} 
 
if(freq_coupon > 0 & freq_coupon <= 1)
{
    freq_coupon_new = 12
} 
 
if(freq_coupon > 1 & freq_coupon <= 2)
{
    freq_coupon_new = 4
} 
 
if(freq_coupon > 2 & freq_coupon <= 3)
{
    freq_coupon_new = 2
} 
 
if(freq_coupon > 3 & freq_coupon <= 4)
{
    freq_coupon_new = 1
} 
 
## COMPUTATIONS
 
terms_coupon_payment  = (seq(1/freq_coupon_new, tenure, by = 
1/freq_coupon_new))*freq_coupon_new
coupon_amount            = coupon/(freq_coupon_new)
cash_flow1                  = rep(c(coupon_amount), (tenure*freq_coupon_new - 
1)) 
cash_flow2                  = par_value + coupon_amount
cash_flow                   = c(cash_flow1, cash_flow2) 
 
ytm_effective              = ((1+ytm/100)^(1/freq_coupon_new))-1
 
pv = NULL
 
for (i in 1:(tenure*freq_coupon_new))
 {
   pv[i] = cash_flow[i] / ((1+ytm_effective)^terms_coupon_payment[i])
 }
 
macaulay_duration = sum(pv*terms_coupon_payment)/sum(pv)
modified_duration = macaulay_duration / (1+(ytm_effective)/freq_coupon_new)

macaulay_duration
modified_duration
 
## _____________________________________________________________________
 
# My PROBLEM
 
Here I am dealing with only one id i.e. only one record. However, if Instead of 
one record,  ahve say 20 records, how do I calculate the Macaulay Duration for 
each of these 20 records. One option is to run this code 20 times *which I 
guess will be foolish thing to do. Other method is to define above code as some 
function and tehn run this function for each of these records, but I don't 
underatnd how to write  a function and thord option is to treat the input of 
these 20 records in a matrix form, which I had tried unsuccessfully.
 
Please guide me as to how do I modify the R-code to calculate Mac duration for 
each of tehse records and store tehm.
 
Regards
 
Madhavi Bhave
 


      The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. 
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to