【问题标题】:Writing a conditional If-statement for portfolio analysis为投资组合分析编写条件 If 语句
【发布时间】:2020-04-22 11:20:15
【问题描述】:

我是 R 新手,目前正在尝试根据特定分数创建等权重的投资组合,但我在部分代码方面遇到了一些问题。我为得分最高和最低的贡献者创建了单独的数据框,并计算了每月回报。数据集现在看起来像这样:

                              Company  Year      Score  Date     Return Market Value
                              Company x 2008     26.26 2008-01-01  -0.32       601.26
                              Company x 2008     26.26 2008-02-01  -0.10       410.65
                              Company x 2008     26.26 2008-03-01  -0.06       369.80
                              Company x 2008     26.26 2008-04-01   0.01       348.15
                              Company x 2008     26.26 2008-05-01   0.28       353.10
                              Company x 2008     26.26 2008-06-01  -0.25       452.43
                              Company x 2008     26.26 2008-07-01   0.11       338.91
                              Company x 2008     26.26 2008-08-01   0.15       376.86
                              Company x 2008     26.26 2008-09-01  -0.12       433.62
                              Company x 2008     26.26 2008-10-01  -0.27       383.72
                              Company x 2008     26.26 2008-11-01  -0.25       281.95
                              Company x 2008     26.26 2008-12-01  -0.17       211.20

我的最终目标是从高投资组合中减去表现不佳的投资组合,看看高投资组合是否会带来异常回报。为此,我想编写一个 if 函数,该函数允许我指定如果 previous 月份的市场价值 > 0,那么我希望我的回报列成倍增加与相应的权重列。我遇到了这个问题,因为我的数据集中的一些公司在这一年中被除名,然后需要在一年中的剩余月份将权重重新分配给剩余的公司。 各种网站都提供了很大的帮助(尤其是这个:https://www.codingfinance.com/post/2018-04-05-portfolio-returns/),但我似乎无法找到解决上述问题的方法。感谢所有帮助,如果代码混乱,我深表歉意,谢谢。

  returnhigh <- highport %>%
    group_by(Company) %>%
    # Trying to add the weight constraint 
    if(test$`Market Value` > 0) {
      # This is the part where I struggle to specify the market value of the previous month 
      returnhigh$Weight <- 1/length(highport$Company)
    }

  returnhigh <- returnhigh %>% mutate(wt_return = Weight * Return)
  porthigh <- returnhigh %>% group_by(Date) %>% summarise(port_ret = sum(wt_return))
  returnlow <- lowport %>%
    group_by(Company) %>%
    if(test$`Market Value` > 0) {
      # Same problem as above 
      returnlow$Weight <- 1/length(lowport$Company)
    }
  returnlow <- returnlow %>% mutate(wt_return = Weight * Return)
  portlow <- returnlow %>% group_by(Date) %>% summarise(port_ret = sum(wt_return))
  result <- porthigh - portlow
  return(result)
}


【问题讨论】:

  • 欢迎来到 SO。人们很难根据您提供的代码来帮助您。添加一些示例数据集和您想要的输出将会很棒。
  • 谢谢弗兰克,我现在添加了更多上下文。如果还有什么我可以提供的,请告诉我。

标签: r if-statement portfolio


【解决方案1】:

很难准确分析出您要寻找的内容,而且我没有金融背景,但这样的事情可能会有所帮助:

library(dplyr)

# High Returns
returnhigh <- df %>%
  group_by(Company) %>%
  filter(`Market Value` > 0) %>%
  mutate(Weight = 1/length(Company)) %>%
  ungroup() %>%
  mutate(wt_return = Weight * Return)

# Low Returns
returnlow <- df %>%
  group_by(Company) %>%
  filter(`Market Value` < 0) %>%
  mutate(Weight = 1/length(Company)) %>%
  ungroup() %>%
  mutate(wt_return = Weight * Return)

# Port Function to summarise your high and low returns

port_func <- function (x) {
  x <- x %>%
    group_by(Date) %>%
    summarise(port_ret = sum(wt_return))
  }

porthigh <- port_func(returnhigh)
portlow <- port_func(returnlow)

输入:

df <- structure(list(Company = c("Company x", "Company x", "Company x", 
"Company x", "Company x", "Company x", "Company x", "Company x", 
"Company x", "Company x", "Company x", "Company x"), Year = c(2008, 
2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008
), Score = c(26.26, 26.26, 26.26, 26.26, 26.26, 26.26, 26.26, 
26.26, 26.26, 26.26, 26.26, 26.26), Date = c("2008-01-01", "2008-02-01", 
"2008-03-01", "2008-04-01", "2008-05-01", "2008-06-01", "2008-07-01", 
"2008-08-01", "2008-09-01", "2008-10-01", "2008-11-01", "2008-12-01"
), Return = c(-0.32, -0.1, -0.06, 0.01, 0.28, -0.25, 0.11, 0.15, 
-0.12, -0.27, -0.25, -0.17), `Market Value` = c(601.26, 410.65, 
369.8, 348.15, 353.1, 452.43, 338.91, 376.86, 433.62, 383.72, 
281.95, 211.2)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 2013-04-23
    • 1970-01-01
    • 2011-08-08
    • 2011-09-12
    • 2018-10-12
    • 2017-02-03
    • 2020-11-11
    相关资源
    最近更新 更多