English 中文(简体)
如何计算来自价格矢量的收益?
原标题:How to calculate returns from a vector of prices?
  • 时间:2011-08-21 20:43:57
  •  标签:
  • r

我必须计算出一个能够提供历史系列存货的病媒的回报。 病媒形式如下:

a <- c(10.25, 11.26, 14, 13.56) 

我需要计算每日收益/损失((%),即从10.25到11.26,然后从11.26到14等。

是否有自动计算这一费用的职能?

最佳回答

根据你的抽样数据,我认为你指的是:

a <- c(10.25, 11.26, 14, 13.56) 
> diff(a)/a[-length(a)]
[1]  0.09853659  0.24333925 -0.03142857

<代码>diff 回归滞后差异的矢量和a [-length(a)] 放弃了a的最后一个要素。

问题回答

您可在<代码>quantmod中找到职能。 与你的工作有关:

> require(quantmod)
> Delt(a)
     Delt.1.arithmetic
[1,]                NA
[2,]        0.09853659
[3,]        0.24333925
[4,]       -0.03142857

你们也可以利用回报与原木回报减价等值的确切关系。 因此,如果<打字码>标/编码>包含贵方的价格,则如下:

Returns = exp(diff(log(Prices))) - 1

请注意,这是一个exact关系,而不是@PBS的答复中给出的大致关系。

更详细的例子有多个时间序列:

############ Vector  ############

vPrice <- (10.25, 11.26, 14, 13.56) 
n = length(vPrice)

#Log returns

log_ret <- diff(log(vPrice)) # or = log(vPrice[-1]/vPrice[-n]) because "..[-i]" removes the i th item of the vector
log_ret

#Simple returns

simple_ret <- diff(vPrice)/vPrice[1:(n-1)] # or = diff(vPrice)/vPrice[-n]
simple_ret


############ Multiple Time series ############

head(EuStockMarkets)

mPrice <-  EuStockMarkets
n = dim(mPrice)[1] #Nb rows

log_ret <- diff(log(mPrice))
head(log_ret)

simple_ret <- diff(mPrice)/mPrice[1:(n-1),]
head(simple_ret)


#Total Returns

total_log_ret <- colSums(log_ret,2) #just a sum for log-returns
total_log_ret
total_Simple_ret <- apply(1+simple_ret, 2, prod)-1 # product of simple returns
total_Simple_ret

##################

#From simple to log returns 
all.equal(log(1+total_Simple_ret),total_log_ret) #should be true

#From Log to simple returns 
all.equal( total_Simple_ret,exp(total_log_ret)-1) #should be true
ret<-diff(log(a))

这将使你们获得几何回报——按日平均分配(较低边界为100%,因为价格总是非否定性),因此,<代码>ln(价格)

对于“正常”的回归范围,<代码>[P(t+1)-P(t)]/P(t)和<代码>LN(P(t+1)/P(t)之间的差额应可忽略不计。 我希望这一帮助。

另一种可能性是<代码>ROC功能 ofTTR 一揽子:

library(TTR)
a <- c(10.25, 11.26, 14, 13.56)
ROC(a, type = "discrete")
## [1]          NA  0.09853659  0.24333925 -0.03142857

<代码> 类型 = 连续(也是缺损的)使回归者:

ROC(a)
## [1]          NA  0.09397892  0.21780071 -0.03193305

你可以这样做:

(PRICE / lag(PRICE)-1) * 100




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签