fatreturns: Simple and Elaborated Prices to Returns

Description Usage Arguments Details Examples

View source: R/b_data.R

Description

fatreturns is an elaborated function to compute prices to returns. It includes a pre-treatment for negative prices. It computes either log-returns (default) or percentage-returns. It handles properly NA values in the input vector, replacing them by 0 in the output vector. Doing so, it warrants that the sum of the log-returns (when selected) is equal to the difference of the log-prices. It works with vector, matrix, data.frame, timeSeries, xts, zoo, list, list of lists and even list of vector, data.frame, timeSeries, xts, zoo mixed together. The returned object is of same dimension and same class than the input object with the first line filled with 0. The results may be as per one, per cent (default), per thousand and per ten thousand.

logreturns is an improved version of function 100*diff(log(x)) to handle vector, matrix, data.frame and list. It handles properly the first line and the NA values. It does not control time, rownames and colnames but may return them.

Usage

1
2
3
4
5
fatreturns(x, log = TRUE, per = "cent", e = NULL, dfrcol = 1, na.rm = TRUE)

logreturns(x)

replaceNA(x)

Arguments

x

The prices (vector, data.frame, matrix, timeSeries, xts, zoo, list).

log

boolean. log returns or percentage returns.

per

character. Either "one", "cent, "thousand", "tenthousand" or "o", "c", "th", "te". Multiply the result by 1, 100, 1000, 10000.

e

NULL or positive numeric. NULL is for no change f(x)=x. A positive numeric designates the focal point of the hyperbola to turn negative prices into positive prices, keeping the hierarchy: f(x)=(x+sqrt(x*x+e*e))/2. There is currently no rules of thumb for the optimal value of e.

dfrcol

integer. For data.frame only, designates the column that handles the time and must be processed separately. Use dfrcol = 0 if all columns must be processed and there is no time (or turn the data.frame to a matrix).

na.rm

boolean. Replace x[t]=NA with the previous non-NA value available in the price serie such that (x[t-1], x[t]=x[t-1], x[t+1]) and calculate the returns accordingly. Force 0 in the first line of the returns if x[1]=NA.

Details

Negative prices in financial markets, like interest rates in Europe, are a nightmare as the rough calculation of the returns generates non-sense values. elevate uses an hyperbola and implements the following formula:

elevate(x, e) = (x + sqrt(x*x + e*e)) / 2

There is currently no rule of thumb to calculate e. When e = NULL, there is no change and the output is identical to the input. When e = 0, all negative values are turned to 0.

Examples

1
2

FatTailsR documentation built on March 12, 2021, 9:06 a.m.