我想计算根据某些特征按季度重新平衡的投资组合的月度加权回报。我是R的新手,非常感谢你的帮助。
投资组合构建规则如下(非常类似于法马和法国的(1993)HML因子组合。
(1)6个月的股票排名滞后/先前的账面市值比(Lagged_BtM)。 (T-6)
(2)具有最高Lagged_BtM的30%股票表示"高"投资组合和具有最低Lagged_BtM的30%股票是"低"投资组合。
(3)根据(2)中的规则,每3个月(每季度)重新平衡投资组合。例如在2000-01-31,以Lagged_BtM的价格买入前30%的股票,持有3个月及以上计算这3个月中每一个的投资组合回报。在2000-04-31再次按Lagged_BtM对股票进行排名,并根据(2)中的规则购买/调整投资组合。依此类推,直到数据集中的指定结束日期为止。 (t,t + 3,t + 6等)
(4)计算加权月回报值。在投资组合形成时按市值(Market_Cap)计算每家公司的回报。要使用上面的示例:在2000-01-31考虑Market_Cap,然后在2000-04-31再考虑等等。
我的数据集如下所示:
Name = c(rep("Company X", 12), rep("Company Y", 12), rep("Company Z", 12))
Date = rep(rev(seq(as.Date("2000-02-01"), as.Date("2001-01-01"), by = "1 month") - 1), 3)
Market_Cap = c(runif(12, 35000, 40000), runif(12, 5000, 8000), runif(12, 20000, 22000)) # Market capitalization
Book_to_Market = c(runif(12, 0.2, 1), runif(12, 0.9, 2), runif(12, 0.6, 1.5))
Total_Return_Index = c(runif(12, 19, 25), runif(12, 74, 82), runif(12, 4, 11))
stocks = data.frame(Name, Date, Market_Cap, Book_to_Market, Total_Return_Index)
然后我添加了一些帮助我计算的列。可能不需要知道R的人。
# Add columns to simplify calculation
library(dplyr)
library(tidyr) # Not sure if I used this
stocks = stocks %>%
group_by(Name) %>%
mutate(Monthly.Return = log(lag(Total_Return_Index, 1) / Total_Return_Index)) %>% # Calculate 1 month (log) returns
mutate(Weighted.Return = Monthly.Return * Market_Cap) %>% # Add column to help me calculate value weighted returns (returns weighted by Market_Cap)
mutate(Lagged_BtM = lead(Book_to_Market, n = 3)) %>% # Add 3 months lagged Book_to_Market (6 months in real data)
mutate(Quarterly.Return = log(lag(Total_Return_Index, 3) / Total_Return_Index)) %>% # Add quarterly return, used later
mutate(q.Weighted.Return = Quarterly.Return * Market_Cap) # Quarterly value weighted return, used later
stocks = with(stocks, subset(stocks, Lagged_BtM != "NA" & Weighted.Return != "NA")) # Remove NA from Lagged_BtM and Weighted.Return
然后我的数据框看起来像这样:
Name Date Market_Cap Book_to_Market Total_Return_Index Monthly.Return Weighted.Return Lagged_BtM
1 Company X 2000-11-30 35565.061 0.4094885 22.24248 0.012044044 428.34715 0.4231916
2 Company X 2000-10-31 39544.379 0.8467753 24.22646 -0.085441292 -3378.72281 0.8682684
3 Company X 2000-09-30 38306.471 0.8340233 20.20816 0.181358708 6947.21209 0.7640378
4 Company X 2000-08-31 36791.679 0.4231916 22.87716 -0.124052523 -4564.10060 0.7609808
5 Company X 2000-07-31 37414.159 0.8682684 19.47549 0.160982536 6023.02618 0.7534791
6 Company X 2000-06-30 37724.365 0.7640378 24.50530 -0.229732765 -8666.52262 0.7444255
7 Company X 2000-05-31 39639.062 0.7609808 21.36683 0.137049816 5432.52619 0.8661182
8 Company X 2000-04-30 37258.858 0.7534791 21.47190 -0.004905545 -182.77501 0.7011865
9 Company Y 2000-11-30 7509.425 1.7420568 74.26786 0.032263995 242.28404 1.5831691
10 Company Y 2000-10-31 5872.622 1.0926830 75.19581 -0.012417092 -72.92088 1.7423244
.. ... ... ... ... ... ... ... ...
Variables not shown: Quarterly.Return (dbl), q.Weighted.Return (dbl)
最后,我制定了我的MONTHLY重新平衡投资组合,并添加了HML投资组合:
## Create monthly rebalanced portfolios
# Create portfolio with low lagged book-to-market ratio
low.BtM = stocks %>%
group_by(Date) %>%
filter(Lagged_BtM < quantile(Lagged_BtM, 0.3)) %>% # Bottom 30% stocks in terms of lagged book-to-market
summarise(
n.low.BtM = length(Weighted.Return), # Number of stocks per portfolio
return.low.BtM = mean(Weighted.Return) / mean(Market_Cap) # Monthly return
)
# Create portfolio with high lagged book-to-market ratio
high.BtM = stocks %>%
group_by(Date) %>%
filter(Lagged_BtM > quantile(Lagged_BtM, 0.7)) %>% # Top 30% stocks in terms of lagged book-to-market
summarise(
n.high.BtM = length(Weighted.Return),
return.high.BtM = mean(Weighted.Return) / mean(Market_Cap)
)
# Combine portfolios in one data frame, add high minus low portfolio (HML)
Portfolios = cbind(low.BtM, high.BtM[ , 2:3])
Portfolios$HML = Portfolios$return.high.BtM - Portfolios$return.low.BtM # Add HML portfolio (return.high.BtM - return.low.BtM)
我得到的是这个,可能(不确定)对于每月重新平衡的投资组合是正确的。
Date n.low.BtM return.low.BtM n.high.BtM return.high.BtM HML
1 2000-04-30 1 -0.004905545 1 0.036513834 0.04141938
2 2000-05-31 1 0.037053475 1 0.017815062 -0.01923841
3 2000-06-30 1 -0.229732765 1 0.051991828 0.28172459
4 2000-07-31 1 0.160982536 1 0.015922870 -0.14505967
5 2000-08-31 1 -0.124052523 1 -0.009022466 0.11503006
6 2000-09-30 1 0.181358708 1 -0.033899042 -0.21525775
7 2000-10-31 1 -0.085441292 1 -0.012417092 0.07302420
8 2000-11-30 1 0.012044044 1 0.032263995 0.02021995
不幸的是,我无法弄清楚如何计算季度重新平衡投资组合的月度回报。如果有什么不清楚我会很乐意编辑它。我希望你能帮助我。谢谢。
当然这种尝试是错误的......
## Create quarterly rebalanced portfolios
stocks = with(stocks, subset(stocks, Lagged_BtM != "NA" & q.Weighted.Return != "NA")) # Remove NA from Lagged_BtM and q.Weighted.Return
# Create portfolio with low lagged book-to-market ratio
low.BtM = stocks %>%
group_by(Date) %>%
filter(Lagged_BtM < quantile(Lagged_BtM, 0.3)) %>%
summarise(
n.low.BtM = length(q.Weighted.Return),
return.low.BtM = mean(q.Weighted.Return) / mean(Market_Cap)
)
# etc.