returns | R Documentation |
Compute log-returns, simple returns and basic differences (or the inverse operations) from given data.
returns(x, method = c("logarithmic", "simple", "diff"), inverse = FALSE,
start, start.date)
returns_qrmtools(x, method = c("logarithmic", "simple", "diff"),
inverse = FALSE, start, start.date)
x |
matrix or vector (possibly a |
method |
|
inverse |
|
start |
if |
start.date |
|
If inverse = FALSE
and x
is an xts
object, the
returned object is an xts
, too.
Note that the R package timeSeries also contains a function
returns()
(and hence the order in which timeSeries and
qrmtools are loaded matters to get the right returns()
).
For this reason, returns_qrmtools()
is an alias for
returns()
from qrmtools.
vector
or matrix
with the same number of
columns as x
just one row less if inverse = FALSE
or one row more if inverse = TRUE
.
Marius Hofert
## Generate two paths of a geometric Brownian motion
S0 <- 10 # current stock price S_0
r <- 0.01 # risk-free annual interest rate
sig <- 0.2 # (constant) annual volatility
T <- 2 # maturity in years
N <- 250 # business days per year
t <- 1:(N*T) # time points to be sampled
npath <- 2 # number of paths
set.seed(271) # for reproducibility
S <- replicate(npath, S0 * exp(cumsum(rnorm(N*T, # sample paths of S_t
mean = (r-sig^2/2)/N,
sd = sqrt((sig^2)/N))))) # (N*T, npath)
## Turn into xts objects
library(xts)
sdate <- as.Date("2000-05-02") # start date
S. <- as.xts(S, order.by = seq(sdate, length.out = N*T, by = "1 week"))
plot(S.[,1], main = "Stock 1")
plot(S.[,2], main = "Stock 2")
### Log-returns ################################################################
## Based on S[,1]
X <- returns(S[,1]) # build log-returns (one element less than S)
Y <- returns(X, inverse = TRUE, start = S[1,1]) # transform back
stopifnot(all.equal(Y, S[,1]))
## Based on S
X <- returns(S) # build log-returns (one element less than S)
Y <- returns(X, inverse = TRUE, start = S[1,]) # transform back
stopifnot(all.equal(Y, S))
## Based on S.[,1]
X <- returns(S.[,1])
Y <- returns(X, inverse = TRUE, start = S.[1,1], start.date = sdate)
stopifnot(all.equal(Y, S.[,1], check.attributes = FALSE))
## Based on S.
X <- returns(S.)
Y <- returns(X, inverse = TRUE, start = S.[1], start.date = sdate)
stopifnot(all.equal(Y, S., check.attributes = FALSE))
## Sign-adjusted (negative) log-returns
X <- -returns(S) # build -log-returns
Y <- returns(-X, inverse = TRUE, start = S[1,]) # transform back
stopifnot(all.equal(Y, S))
### Simple returns #############################################################
## Simple returns based on S
X <- returns(S, method = "simple")
Y <- returns(X, method = "simple", inverse = TRUE, start = S[1,])
stopifnot(all.equal(Y, S))
## Simple returns based on S.
X <- returns(S., method = "simple")
Y <- returns(X, method = "simple", inverse = TRUE, start = S.[1,],
start.date = sdate)
stopifnot(all.equal(Y, S., check.attributes = FALSE))
## Sign-adjusted (negative) simple returns
X <- -returns(S, method = "simple")
Y <- returns(-X, method = "simple", inverse = TRUE, start = S[1,])
stopifnot(all.equal(Y, S))
### Basic differences ##########################################################
## Basic differences based on S
X <- returns(S, method = "diff")
Y <- returns(X, method = "diff", inverse = TRUE, start = S[1,])
stopifnot(all.equal(Y, S))
## Basic differences based on S.
X <- returns(S., method = "diff")
Y <- returns(X, method = "diff", inverse = TRUE, start = S.[1,],
start.date = sdate)
stopifnot(all.equal(Y, S., check.attributes = FALSE))
## Sign-adjusted (negative) basic differences
X <- -returns(S, method = "diff")
Y <- returns(-X, method = "diff", inverse = TRUE, start = S[1,])
stopifnot(all.equal(Y, S))
### Vector-case of 'method' ####################################################
X <- returns(S., method = c("logarithmic", "diff"))
Y <- returns(X, method = c("logarithmic", "diff"), inverse = TRUE, start = S.[1,],
start.date = sdate)
stopifnot(all.equal(Y, S., check.attributes = FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.