R 中多个时间段的多种证券的对数回报 [英] Log returns of multiple securities for multiple time period in R

查看:34
本文介绍了R 中多个时间段的多种证券的对数回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含 5413 家公司从 2000 年到 2014 年的每日收盘价的数据集.我想根据日志日期(今天的价格/昨天的价格)计算股票的每日对数回报.我将数据集说明如下:

I have dataset containing daily closing prices of 5413 companies from 2000 to 2014. I want to calculate daily log returns for the stocks as according to dates as log(Price today/Price yesterday). I illustrate the dataset as follows:

Date       A G L    ABA    ABB ABBEY 
2000-1-3    NA      NA      NA  NA
2000-1-4    79.5    325     NA  961  
2000-1-5    79.5    322.5   NA  945
2000-1-6    79.5    327.5   NA  952
2000-1-7    NA      327.5   NA  941  
2000-1-10   79.5    327.5   NA  946
2000-1-11   79.5    327.5   NA  888

如何计算每日对数回报并另外解决 NA.我的样本期是从2000年到2014年,所以有一些公司是在2001年上市的,所以,2000年全年都是NA,应该如何处理.非常感谢您的帮助.

How could calculate the the daily log returns and additionally tackle the NA. My sample period is from 2000 to 2014 so there are some companies who were listed in year 2001,so, for the whole year 2000 they have NA, how should this be handled. Your help is highly appreciated.

推荐答案

根据 这个话题 我会试着描述我的主张:

According to this topic I'll try to describe my proposition:

据我所知,我们有一个包含日期和数千家公司的数据框.这是我们名为 prices 的示例数据框:

What I understand is that we have got a dataframe of dates and thousands of companies. Here's our example dataframe called prices:

> prices
    newdates nsp1  nsp2 nsp3 nsp4
1 2000-01-03   NA    NA   NA   NA
2 2000-01-04 79.5 325.0   NA  961
3 2000-01-05 79.5 322.5   NA  945
4 2000-01-06 79.5 327.5   NA  952
5 2000-01-07   NA 327.5   NA  941
6 2000-01-10 79.5 327.5   NA  946
7 2000-01-11 79.5 327.5   NA  888

要创建一个新的日志返回数据框,我使用了以下代码:

To create a new dataframe of log-returns I used below code:

logs=data.frame(
+   cbind.data.frame(
+     newdates[-1],
+     diff(as.matrix(log(prices[,-1])))
+     )
+   )
> logs
  newdates..1. nsp1         nsp2 nsp3         nsp4
1   2000-01-04   NA           NA   NA           NA
2   2000-01-05    0 -0.007722046   NA -0.016789481
3   2000-01-06    0  0.015384919   NA  0.007380107
4   2000-01-07   NA  0.000000000   NA -0.011621895
5   2000-01-10   NA  0.000000000   NA  0.005299429
6   2000-01-11    0  0.000000000   NA -0.063270826

为了阐明这段代码中发生了什么,让我们从内而外分析它:

第 1 步:计算对数回报

To clarify what is going on in this code lets analyze it from the inside out:

Step 1: Calculating log-returns

  • 你知道log(a/b) = log(a)-log(b),所以我们可以计算对数的差异.函数 diff(x,lag=1) 计算给定滞后的差异.这里是 lag=1 所以它首先给出差异.
  • 我们的 x 是数据框中的价格.从一个data.frame 没有我们使用的第一个(有日期)的每一列价格[,-1].
  • 我们需要对数,所以log(prices[,-1])
  • 函数 diff() 适用于向量或矩阵,因此我们需要处理计算对数作为矩阵,因此`as.matrix(log(prices[,-1]))
  • 现在我们可以使用 diff()lag=1,所以 diff(as.matrix(log(prices[,-1])))
  • You know that log(a/b) = log(a)-log(b), so we can calculate differences of logarithms. Funcition diff(x,lag=1) calculates differences with given lag. Here it is lag=1 so it gives first differences.
  • Our x are prices in dataframe. Do pick from a data.frame every columns without the first (there are dates) we use prices[,-1].
  • We need logarithms, so log(prices[,-1])
  • Function diff() works with vector or matrix, so we need to treat calculated logarithms as matrix, thus `as.matrix(log(prices[,-1]))
  • Now we can use diff() with lag=1, so diff(as.matrix(log(prices[,-1])))

第 2 步:创建日志返回和日期的数据框

  • 我们不能只使用 cbind().首先,因为长度不同(返回短 1 条记录).我们需要删除第一个日期,所以 newdates[-1]

  • We can't use just cbind(). Firstly, because lengths are different (returns are shorter by 1 record). We need to remove first date, so newdates[-1]

其次,使用 cbind() 日期将被转换为数字值,例如 160027 或其他.
这里我们必须使用 cbind.data.frame(x,y),如上所示.

Secondly, using cbind() dates will be transformed into numeric values such 160027 or other.
Here we have to use cbind.data.frame(x,y), as seen above.

现在数据已经准备好了,我们可以创建一个 data.frame() 并将其命名为日志,所以 logs=data.frame(...) 同上.

Now data is ready and we can create use a data.frame() and name it as logs so logs=data.frame(...) as above.

如果您的数据集看起来像数据框 prices 它应该运行.最重要的是使用 diff(log(x)) 来轻松计算 log-returns.

If your dataset look like dataframe prices it should run. Most important thing is to use diff(log(x)) to easily calculate log-returns.

如果您有任何疑问或问题,请直接提问.

If you have any questions or problem, then just ask.

这篇关于R 中多个时间段的多种证券的对数回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆