[英]Calculate average monthly returns in data.table with differing number of stocks in each month
Suppose I have a data.table, priceDT, with daily observations of returns on multiple shares like so:假设我有一个 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
I want to calculate the average monthly return of all shares in a given month.我想计算给定月份所有股票的平均月回报率。 So in January of 2011, the average of the return of the two shares.
所以在 2011 年 1 月,两只股票的平均回报率。 We know it's only two shares because of the share column.
由于份额列,我们知道它只有两股。 The first step is to get the average return of each share in that month.
第一步是获取当月每股的平均回报。 Then get the average return of the portfolio of shares in that month.
然后得到当月股票组合的平均回报。 So in January, the average of GAI is 0.02019333 and the average of KDV is 0.02523333.
所以一月份,GAI的平均值是0.02019333,KDV的平均值是0.02523333。 The average for the month is therefore: 0.02019333
因此,该月的平均值为:0.02019333
That is the logic of the portfolio return.这就是投资组合回报的逻辑。 I want to repeat in data.table for the rest of the months
我想在 data.table 重复月份的 rest
For my sample data, I want a result like so:对于我的样本数据,我想要这样的结果:
portfolio
Date avg_return
1: 2011-01 0.02271333
2: 2011-02 -0.008700561
Data:数据:
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
")
Here is another approach, though my results do not match yours.这是另一种方法,尽管我的结果与您的结果不符。
You can create a "year-month" column for grouping results.您可以创建一个“年-月”列来对结果进行分组。 Following your steps, you can calculate the average share for each month (for each share), we'll call that
ShareMean
.按照您的步骤,您可以计算每个月的平均份额(对于每个份额),我们称之为
ShareMean
。
Then, you can calculate the average of these means, across all shares for a given month, we'll call that MonthMean
.然后,您可以计算给定月份所有份额的这些均值的平均值,我们将其称为
MonthMean
。
Is this what you had in mind?这是你的想法吗?
library(data.table)
priceDT[, YearMonth := list(substr(Date, 1, 7))]
priceDT[, .(ShareMean = mean(Return)), by = c("YearMonth", "Share")][
, .(MonthMean = mean(ShareMean)), by = "YearMonth"]
Output Output
YearMonth MonthMean
1: 2011-01 0.022713333
2: 2011-02 -0.008700561
You can can calculate the montly return directly, i would do this like that:你可以直接计算每月的回报,我会这样做:
library(tidyverse)
library(lubridate)
priceDT %>%
mutate(month = month.abb[month(Date)]) %>%
group_by(month) %>%
summarise(avg_return = mean(Return))
( month.abb[month(Date)]
for month abbreviation eg Jan, Feb) (
month.abb[month(Date)]
用于月份缩写,例如 Jan、Feb)
or first calculate the average of a share for a given month:或首先计算给定月份的平均份额:
priceDT %>%
mutate(month = month.abb[month(Date)]) %>%
group_by(month,Share) %>%
summarise(avg_return = mean(Return))
and then you can calculate the average monthly return as above.然后你可以像上面那样计算平均月收益。
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
I do not see how you make the step t get your monthly average return for all sharesd.... but maybe this will get you started?我看不出你是如何迈出这一步来获得所有共享的每月平均回报的……但也许这会让你开始?
#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 ) ]
but fro here, I cannot see the logic to get to portfolio
provided...但是从这里开始,我看不到提供的
portfolio
的逻辑......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.