R公司的投资组合构建与不同的再平衡期
我想计算根据某些特征按季度重新平衡的投资组合的月度价值加权回报。我是R的新手,非常感谢你的帮助 投资组合构造规则如下(与Fama和French(1993)的HML因子投资组合非常相似。R公司的投资组合构建与不同的再平衡期,r,finance,portfolio,R,Finance,Portfolio,我想计算根据某些特征按季度重新平衡的投资组合的月度价值加权回报。我是R的新手,非常感谢你的帮助 投资组合构造规则如下(与Fama和French(1993)的HML因子投资组合非常相似。 (1) 按滞后6个月/之前账面市值比率(滞后)对股票进行排名。(t-6) (2) 滞后时间最大的30%的股票代表“高”投资组合,滞后时间最小的30%的股票代表“低”投资组合。 (3) 根据(2)中的规则,每3个月(每个季度)重新平衡投资组合。例如,在2000-01-31年购买滞后时间最多的30%股票,持有3个月,
(1) 按滞后6个月/之前账面市值比率(滞后)对股票进行排名。(t-6)
(2) 滞后时间最大的30%的股票代表“高”投资组合,滞后时间最小的30%的股票代表“低”投资组合。
(3) 根据(2)中的规则,每3个月(每个季度)重新平衡投资组合。例如,在2000-01-31年购买滞后时间最多的30%股票,持有3个月,并计算这3个月的投资组合回报。在2000-04-31年,再次按滞后时间对股票进行排名,并根据(2)中的规则购买/调整投资组合。依此类推,直到数据集中指定的结束日期。(t、t+3、t+6等)
(4)计算价值加权的月度收益率,利用投资组合的形成时间来衡量各公司的市场回报率(MauleSCAP)。 我的数据集如下所示:
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)
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)
然后我添加了一些列来帮助我计算。可能不需要知道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 = 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)
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)
最后,我计算每月重新平衡的投资组合,并添加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)
不幸的是,我不知道如何计算季度再平衡投资组合的月度回报。如果有任何不清楚的地方,我将很乐意编辑。我希望您能帮助我。谢谢
当然,这种尝试是错误的
## 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.
##创建季度再平衡投资组合
股票=带有(股票,子集(股票,滞后的BtM!=“NA”&q.Weighted.Return!=“NA”))#从滞后的BtM和q.Weighted.Return中删除NA
#创建低滞后账面市盈率的投资组合
low.BtM=股票%>%
分组单位(日期)%>%
滤波器(滞后的BtM<分位数(滞后的BtM,0.3))%>%
总结(
n、 low.BtM=长度(q加权返回),
return.low.BtM=平均值(q.加权回报)/平均值(市值)
)
#等等。
为什么按(日期)分组?如果你想按季度重新平衡,你不应该使用按(年度)分组吗
?其中:where:year\u qtr=lubridate::quarty(Date,with\u year=TRUE)
。我对财务很陌生,所以可能不完全理解你的问题。