本文介绍了计算一段时间内的回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得在特定时间持有某种资产的时间序列回报.

I'm trying to get a time series of returns for holding a certain asset for a specific time.

我的数据框如下所示:

Date          Price
1998-01-01     20
1998-01-02     22
1998-01-03     21
1998-01-04     25
...
1998-01-20     25
1998-01-21     19
1998-01-21     20
....
1998-02-01     30
1998-02-02     28
1998-02-03     25
1998-02-04     26
etc.

我每天有 1 次观察,我的时间序列从 1998 年到 1999 年.

I have 1 observation for each day and my time series goes from 1998-1999.

我现在想要做的是计算持有我的资产 20 天的回报(即在第一天买入并在第 20 天卖出),并且每天都这样做.所以我想计算一下:

What I would like to do now is calculate a return for holding my asset for 20 days (i.e. buying it at the first day and selling it at day 20), and do this for each day. So I would like to calculate this:

1.day: Return(20days) = log (Price(t=20)/Price (t=0)),

1.day: Return(20days) = log (Price(t=20) / Price (t=0)),

2.day: Return(20days) = log (Price(t=21)/Price (t=1)),

2.day: Return(20days) = log (Price(t=21) / Price (t=1)),

3.day: Return(20days) = log (Price(t=22)/Price (t=2))

3.day: Return(20days) = log (Price(t=22) / Price (t=2))

等,即在我的样本中每天都这样做.

etc., i.e. do this for every day in my sample.

因此,我生成的数据框将如下所示:

So, my resulting dataframe would look like this:

Date          Return
1998-01-01     0.2
1998-01-02     0.4
1998-01-03     0.6
1998-01-04     0.1
...
1998-01-20     0.1
1998-01-21     0.2
1998-01-21     0.5
....
1998-02-01     0.1
1998-02-02     0.2
1998-02-03     0.5
1998-02-04     0.01
etc.

R 中有没有办法说:取前 20 个观察值,计算回报.取观察2-21,计算收益.取观察3-22,计算收益等?

Is there a way in R to say: take the first 20 observations, calculate the return. Take observation 2-21, calculate the return. Take observation 3-22, calculate the return etc.?

我完全被困住了,希望得到一些帮助.谢谢!丹妮

I'm totally stuck and would appreciate some help. Thanks!Dani

推荐答案

我建议切换到时间序列类,例如 xtszoo.但是,如果您只是想完成它,并在以后了解更多信息,则可以将其作为数据框轻松完成.请注意,我必须用 NA 填充返回向量以使其正确排列,并且 hold 为 20 确实在 1 上买入并在 1 + 20 上卖出:

I suggest switching to a time series class, like xts or zoo. But if you just want to get it done, and learn more later, you can do it pretty easily as a data frame. Note that I have to pad the return vectors with NAs to make it line up correctly and that a hold of 20 really buy on 1 and sells on 1 + 20:

> library(xts)
> set.seed(2001)
> n <- 50
> hold <- 20
> price <- rep(55, n)
> walk <- rnorm(n)
> for (i in 2:n) price[i] <- price[i-1] + walk[i]
> data <- data.frame(date=as.Date("2001-05-25") + seq(n), price=price)
> data <- transform(data, return=c(diff(log(price), lag=hold), rep(NA, hold)))

如果你准备好使用 xtszoo(这应该适用于任何一个),那么我建议使用 rollapply 来获得转发看看(假设您想要前瞻性的回报,这使得在今天形成投资组合并了解它在未来如何运作变得更加容易):

If you're ready for xts or zoo (this should work in either), then I suggest using rollapply to get the forward look (assuming you want the forward looking return, which makes it a lot easier to form portfolios today and see how it works into the future):

> data.xts <- xts(data[, -1], data[, 1])
> f <- function(x) log(tail(x, 1)) - log(head(x, 1))
> data.xts$returns.xts <- rollapply(data.xts$price, FUN=f, width=hold+1, align="left", na.pad=T)

这两种方法是一样的:

> head(data.xts, hold+2)
         price       return  returns.xts
 [1,] 55.00000  0.026746496  0.026746496
 [2,] 54.22219  0.029114744  0.029114744
 [3,] 53.19811  0.047663206  0.047663206
 [4,] 53.50088  0.046470723  0.046470723
 [5,] 53.85202  0.041843116  0.041843116
 [6,] 54.75061  0.018464467  0.018464467
 [7,] 55.52704 -0.001105607 -0.001105607
 [8,] 56.15930 -0.024183803 -0.024183803
 [9,] 56.61779 -0.010757559 -0.010757559
[10,] 55.51042  0.005494771  0.005494771
[11,] 55.17217  0.044864991  0.044864991
[12,] 56.07005  0.025411005  0.025411005
[13,] 55.47287  0.052408720  0.052408720
[14,] 56.10754  0.034089602  0.034089602
[15,] 56.35584  0.075726190  0.075726190
[16,] 56.40290  0.072824657  0.072824657
[17,] 56.05761  0.070589032  0.070589032
[18,] 55.93916  0.069936575  0.069936575
[19,] 56.50367  0.081570964  0.081570964
[20,] 56.12105  0.116041931  0.116041931
[21,] 56.49091  0.095520517  0.095520517
[22,] 55.82406  0.137245367  0.137245367

这篇关于计算一段时间内的回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:16
查看更多