0

for example, I have 8 couple payment periods. for each row , I would like to get a present value. should I use tapply? sapply? would you please show how I should deal with it. thanks in advance enter image description here

  • Please include an example. – Kristofersen Mar 15 '17 at 15:19
  • row is the interest rate, coupon payment =40, and the last payment = 1040. there are 8 periods. I would like to get a present value for each row – HKU_Johnny_QFin Mar 15 '17 at 15:22
  • 1
    Don't post pictures of data; include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). How do you determine "present value." Show the desired ouput. – MrFlick Mar 15 '17 at 15:22
  • row is the interest rate, coupon payment =40, and the last payment = 1040. there are 8 periods. I would like to get a present value for each row – HKU_Johnny_QFin Mar 15 '17 at 15:22
  • @HKU_Johnny_QFin that's easy for you to follow since you've seen the data but it's much harder to imagine. Please just include the first few rows of your data and an example of the output you'd like to see and it will be much easier to help you. – Kristofersen Mar 15 '17 at 15:23
  • present value = 40/(1+first interest rate)^0.5 + 40/(1+second interest rate)^1. + 40 /(1+third i.r)^1.5 .....+40/(1+7th ir)^3.5 +1040/(1+8th ir)^4 – HKU_Johnny_QFin Mar 15 '17 at 15:24
  • @MrFlick plz see the above description, sorry I am a newb – HKU_Johnny_QFin Mar 15 '17 at 15:28

1 Answers1

0

I'm assuming your data looks like this:

IR1 IR2 IR3 IR4 IR5 IR6 IR7 IR8 Coupon lastPayment
  #   #   #   #   #   #   #   #     40        1040

Let your data be called dat. Then, you can find your presentvalue by running this.

dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4

A function example:

doStuff = function(dat){
    dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4

    return(dat)
}

Since this is for a simulation you're really going to want to run something like this.

for(i in 1:nSim){
    dat = generateDat
    dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4
}

You don't need a function to calculate the presentvalue. the simulation will be setup the same everytime so just add it in the for loop below where you generate your data.

Kristofersen
  • 2,736
  • 1
  • 15
  • 31