diamanratio <- function (x, timeframe = c("daily", "weekly", "monthly"), type = c("log", "lin")) {
# This function computes the Diaman Ratio of the univariate time series (or vector) x.
#
# Usage:
# diamanratio(x, timeframe = c("daily", "weekly", "monthly"), type = c("log", "lin"))
#
# Arguments:
# x a numeric vector or univariate time series.
# timeframe the frequency to be used in the calculation. See 'Details'.
# type "log" or "lin". See 'Details'.
#
# Details:
# By default timeframe = "daily" and type = "log".
# It is done a regression between the ln(x) and time; beta and R2 are calculated. The Diaman
# Ratio is the product between beta and R^2.
#
# For computing the Diaman Ratio for different timeframes can be used:
# weekly (timeframe = "weekly") and monthly (timeframe = "monthly").
#
# If you want to calculate the linear Diaman Ratio then we have to use type = "lin"
#
# Value:
# diamanratio double representing the Diaman Ratio.
#
# Verision:
# 1.0.1 - 29 Set 2017
#
# Author:
# Francesco Canella, Diaman
#
# Reference:
# Bernardi, Daniele and Bertelli, Ruggero, Diaman Ratio (February 16, 2011). Available at SSRN: https://ssrn.com/abstract=1802332 or http://dx.doi.org/10.2139/ssrn.1802332
y <- log(x)
tf <- match.arg(timeframe)
check <- c("daily", "weekly", "monthly") == tf
k <- sum(check * c(260, 52, 12))
tp <- match.arg(type)
x <- seq(0,(NROW(y)-1)/k,1/k)
beta <- cov(x,y)/var(x)
R2 <- cor(x,y)^2
if (tp == "lin")
diamanratio <- exp(beta)^R2-1
else
diamanratio <- beta*R2
return(diamanratio)
}