Calculate Returns over Period of Time

前端 未结 6 604
[愿得一人]
[愿得一人] 2021-02-04 09:51

I\'m trying to get a time series of returns for holding a certain asset for a specific time.

My dataframe looks like this:

Date          Price
1998-01-01         


        
6条回答
  •  囚心锁ツ
    2021-02-04 10:37

    Sample Data

    price <- matrix(c(20,22,21,25,25,19,20,30,28,25,26,27,30,32,31,30),ncol= 1);
    

    Calculate 1 day Log Return

    OneDayLogReturn <- c(diff(log(price)));
    

    Calculate 10 days Log Return

    TenDaysLogReturn <- c(diff(log(price),10))
    

    results:

    0.2623643 0.2047944 0.3566749 0.2468601 0.2151114 0.4567584
    

    verify by :

    for (i in 1:6) {print(log(price[10+i]/price[i]))}
    

    Similarly, 20 Days return can be calculated using larger sample date and use

    c(diff(log(price),20))
    

    or in your case

    c(diff(log(price$Return),20))
    

提交回复
热议问题