【问题标题】:Calculate average monthly returns in data.table with differing number of stocks in each month计算每个月不同股票数量的data.table中的平均月收益
【发布时间】:2021-01-08 13:11:00
【问题描述】:

假设我有一个 data.table,priceDT,每天观察多个股票的回报,如下所示:

> priceDT
          Date      Return Share
 1: 2011-01-03  0.04500000   GAI
 2: 2011-01-03 -0.02100000   KDV
 3: 2011-01-04  0.03300000   GAI
 4: 2011-01-04  0.01770000   KDV
 5: 2011-01-05 -0.01742000   GAI
 6: 2011-01-05  0.07900000   KDV
 7: 2011-02-06  0.02400000   GAI
 8: 2011-02-06 -0.02110000   KDV
 9: 2011-02-07 -0.04300000   AFT
10: 2011-02-07  0.01199700   AIP
11: 2011-02-07  0.00551810   ARH
12: 2011-02-07  0.07451101   BIK
13: 2011-02-07 -0.03495597   BLU
14: 2011-02-07 -0.06062462   CGR
15: 2011-02-07 -0.03660000   GAI
16: 2011-02-07 -0.01240000   KDV

我想计算给定月份所有股票的平均每月回报。所以在2011年1月,这两只股票的平均回报率。由于 share 列,我们知道它只有两个共享。第一步是得到当月每股的平均回报。然后得到当月股票组合的平均收益。所以一月份,GAI 的平均值是 0.02019333,KDV 的平均值是 0.02523333。因此,该月的平均值为:0.02019333

这就是投资组合回报的逻辑。我想在剩下的几个月里在 data.table 中重复

对于我的示例数据,我想要这样的结果:

portfolio

Date  avg_return
1: 2011-01  0.02271333
2: 2011-02 -0.008700561

数据:

priceDT <- fread(text = "Date, Return, Share
                 2011-01-03,0.045,GAI
                 2011-01-03,-0.021,KDV
                 2011-01-04,0.033,GAI
                 2011-01-04,0.0177,KDV
                 2011-01-05,-0.01742,GAI
                 2011-01-05,0.079,KDV
                 2011-02-06,0.024,GAI
                 2011-02-06,-0.0211,KDV
                 2011-02-07,-0.043,AFT
                 2011-02-07,0.011997,AIP
                 2011-02-07,0.0055181,ARH
                 2011-02-07,0.074511006,BIK
                 2011-02-07,-0.034955973,BLU
                 2011-02-07,-0.060624622,CGR
                 2011-02-07,-0.0366,GAI
                 2011-02-07,-0.0124,KDV
                 ")

portfolio <- fread(text = "Date, avg_return
                   2011-01,0.022713333
                   2011-02,-0.01194431
                   ")

【问题讨论】:

  • 这有意义吗the average of GAI is 0.02019333 and the average of KDV is 0.02523333. The average for the month is therefore: 0.02019333

标签: r date data.table finance portfolio


【解决方案1】:
priceDT[, mean(Return), by = .(ym = format(Date, "%Y-%m"), Share)
        ][, mean(V1), by = ym]
#         ym           V1
# 1: 2011-01  0.022713333
# 2: 2011-02 -0.008700561

【讨论】:

    【解决方案2】:

    这是另一种方法,虽然我的结果与你的不匹配。

    您可以创建一个“年-月”列来对结果进行分组。按照您的步骤,您可以计算每个月(每个份额)的平均份额,我们称之为ShareMean

    然后,您可以计算给定月份所有股票的这些平均值的平均值,我们称之为MonthMean

    这是你的想法吗?

    library(data.table)
    
    priceDT[, YearMonth := list(substr(Date, 1, 7))]
    priceDT[, .(ShareMean = mean(Return)), by = c("YearMonth", "Share")][
            , .(MonthMean = mean(ShareMean)), by = "YearMonth"]
    

    输出

       YearMonth    MonthMean
    1:   2011-01  0.022713333
    2:   2011-02 -0.008700561
    

    【讨论】:

    • 是的,这就是我想要的。这符合逻辑。我的输出不正确。希望我能接受这两个答案:(
    【解决方案3】:

    你可以直接计算每月的回报,我会这样做:

    library(tidyverse)
    library(lubridate)
    
    priceDT %>%
    mutate(month =  month.abb[month(Date)]) %>%
    group_by(month) %>%
    summarise(avg_return = mean(Return))
    

    month.abb[month(Date)] 表示月份缩写,例如 Jan、Feb)

    或首先计算给定月份的平均份额:

    priceDT %>%
     mutate(month =  month.abb[month(Date)]) %>%
     group_by(month,Share) %>%
     summarise(avg_return = mean(Return))
    

    然后您可以按上述计算平均每月回报。

    【讨论】:

    • 漂亮的 tidyverse 方法,整洁。
    【解决方案4】:

    我不明白你是如何获得所有股票的月平均回报的。... 但也许这会让你开始?

    #make dates
    priceDT[, Date := as.Date( Date ) ]
    # step 1: mean by share by month
    priceDT[, .(avg_return = mean( Return, na.rm = TRUE) ), 
            by = .( month = format(Date, "%Y-%m"), Share ) ]
    

    但是从这里,我看不到提供的portfolio 的逻辑...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2020-07-13
      • 2014-06-30
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多