Matrix operations work "the other way" to what you expect on rows and 
columns.  Try

## Your data:
time <- c(1, 2, 3)  # Please don't use `t' as a variable name
cash_flow <- c(7, 7, 107)
zero_rate <- data.frame(rating=factor(c("AAA","AA","A","B")),
                         year1=c(3.60, 3.65, 3.72, 4.10),
                         year2=c(4.17,4.22,4.32,4.67),
                         year3=c(4.73,4.78,4.93,5.25))
zero_rate1 <- zero_rate[, -1]
## Make clear we are dealing with matrices (not really needed)
zero_rate2 <- as.matrix(zero_rate1)

## The calculation
colSums(cash_flow/(t(1 + zero_rate2 / 100)^time))
# [1] 106.3549 106.2122 105.7969 104.8871

Here t() is the transpose function.  Try to step through each part of 
the calculation to figure it out.  You may also want to look at c(1, 2, 
3) / matrix(1, 3, 3) and matrix(sqrt(2), 3, 3)^c(1, 2, 4) for smaller 
examples.

Hope this helps a little.

Allan.

On 20/08/10 12:24, Sarah Sanchez wrote:
> Dear R Helpers
>
> I have following data -
>
> cash_flow = c(7, 7, 107)  # 107 = Principle 100 + interest of 7%
> t = c(1,2,3)
>
> and zero rate table as
>
> rating         year1           year2           year3
> AAA            3.60            4.17              4.73
> AA              3.65            4.22             
>   4.78
> A                 3.72           4.32              4.93
> BBB            4.10            4.67             5.25
>
>
> For each of these ratings I need to calculate the Present Value as (say e.g. 
> for AAA)
>
> PV(AAA)  =  7/(1+3.60/100) + 7/(1+4.17/100)^2 +  107/(1+4.73/100)^3 which is 
> equal to 106.3549
>
> Similarly when used the respective rates,  PV(A) = 106.2122, PV(A) = 105.7969 
> and PV(BBB) = 104.8871
>
> #########################################################
>
> ## My problem
>
> I have tried the following R code.
>
> zero_rate =
>   read.csv('zero_rate_table.csv')
> zero_rate1 = zero_rate[, -1]
>
> cash_flow = c(7, 7, 107)
>
> t = c(1,2,3)
>
>
> PV_table = cash_flow / (1+zero_rate1/100)^t
>
> ## Then using rowSums, I should get the required result. However, I am 
> getting following output as
>
>    
>> PV_table
>>      
>          year_1        year_2       year_3
> [1,]  6.756757     6.45078      93.147342
> [2,]  6.515675   94.521493      6.680664
> [3,] 95.895064    6.710123      6.357680
> [4,]   6.724304   6.389305     91.773536
>
>
> rowSums(PV_table) gives
>
> [1] 106.35489 107.71783 108.96287 104.88714.
>
> Thus, the result is correct only in first case. In second case onwards, there 
> is a shift of final cashflow e.g. in 2nd case, pertaining to year 2, value is 
> 94.521493 which means it includes principal of 100.
>
> Likewise
>   in third case, the principal is included in the first cash-flow itself. 
> Again the fourth result is correct. So there is pre-shift of capital.
>
> I am not sure whether I am making any mistake in reading the zero_rate csv 
> file or my formula is incorrect. Please guide.
>
> Thanking you all in advance
>
> Sarah
>
>
>   
>
>
>
>
>
>
>       [[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.
>    

        [[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