
# I have written this code in Notepad++ and copied here.

## ONS - PPA    

Duration = function(par_value, coupon_rate, freq_coupon, tenure, ytm)

{
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                        = coupon_rate*par_value/100
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)

return(data.frame(macaulay_duration, modified_duration))

}


result = Duration(par_value = 1000, coupon_rate = 10, freq_coupon = 0, tenure = 5, ytm = 12)

## _______

When I run this function, I get the values of Macaulay Duration and Modified Duration

> result
  macaulay_duration modified_duration
1          1423.797          1423.795


### MY PROBLEM

I have arrived at a result using only one set of observations i.e. for the following data -

Duration(par_value = 1000, coupon_rate = 10, freq_coupon = 0, tenure = 5, ytm = 12)

However, if I need to obtain these results for multiple records, how do I calculate and obtain the result in a tabular form?

e.g. suppose my input data file is 'instrument details.csv' given as

id     par_value     coupon_rate  frequency_coupon    tenure    ytm
1       1000                    10                         0                     5          12                                
2       100                        7                         1                     8          11            

### frequency_coupon is coded s.t. if frequency_coupon = 0, no of compoundings in a year = 365 and if it is 1, then no of compoundings = 12


Then how do modify the above code?

I have tried to convert in a matrix form as follows

I have added following code after the function is defined i.e. after

#return(data.frame(macaulay_duration, modified_duration))



#}


# Added code

ONS  = read.csv('instrument details.csv')

n      = length(ONS$par_value)

par_value      =  matrix(data = ONS$par_value, nrow = n, ncol = 1, byrow = TRUE)
coupon_rate    =  matrix(data = ONS$coupon_rate, nrow = n, ncol = 1, byrow = TRUE)
freq_coupon  =  matrix(data = ONS$frequency_copoun, nrow = n, ncol = 1, byrow = TRUE)
tenure          =  matrix(data = ONS$tenure, nrow = n, ncol = 1, byrow = TRUE)
ytm             =   matrix(data = ONS$ytm, nrow = n, ncol = 1, byrow = TRUE)

result          =   matrix(data = NA, nrow = n, ncol = 2, byrow = TRUE)

result = Duration(par_value, coupon_rate, freq_coupon, tenure, ytm)

## ________________________________________________________

When I run result, besides getting 50 warnings, I get following 

> result
  macaulay_duration modified_duration
1          826.9026          826.9019
2          826.9026          826.9019

which is I know wrong.

Is there any other way I can use the function defined above to process multiple recrds.

Thanking you and sincerely apologize for writing such a long mail as I wanted to be clear in my communication.

Regards

Madhavi Bhave



ONS  = read.csv('instrument details.csv')

n      = length(ONS$par_value)

par_value      =  matrix(data = ONS$par_value, nrow = n, ncol = 1, byrow = TRUE)
coupon_rate    =  matrix(data = ONS$coupon_rate, nrow = n, ncol = 1, byrow = TRUE)
freq_coupon  =  matrix(data = ONS$frequency_copoun, nrow = n, ncol = 1, byrow = TRUE)
tenure          =  matrix(data = ONS$tenure, nrow = n, ncol = 1, byrow = TRUE)
ytm             =   matrix(data = ONS$ytm, nrow = n, ncol = 1, byrow = TRUE)

result          =   matrix(data = ONS$par_value, nrow = n, ncol = 2, byrow = TRUE)

result = Duration(par_value, coupon_rate, freq_coupon, tenure, ytm)




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

