tidyquant
tidyquant copied to clipboard
tq_performance cannot pass vector for Rf, PerformanceAnalytics does
In the performance analytics package, "Rf" is a variable that can be passed as a single value which tq_performance accepts, or a vector, which tq_performance does not.
set.seed(12345)
RaRf<-tibble(date=as.Date(as.yearmon("2009-01-01")+seq(1/12,3,1/12),frac=1),
Ra=rnorm(3*12,.06/12,.01),
Rf=rnorm(3*12,.02/12,.001))
RaRf_xts<-as_xts(RaRf,date_col = date)
#in tidyquant -------------------------
# vector for Rf NOT accepted
RaRf %>%
tq_performance(Ra = Ra, Rf = Rf, performance_fun = SharpeRatio)
#Error in tq_performance_.tbl_df(data = data, Ra = lazyeval::expr_text(Ra), :
#object 'ret' not found
#In addition: Warning message:
# In fun_performance(eval(parse(text = Ra)), ...) : object 'Rf' not found
#in PerformanceAnalytics -------------------------
# vector for Rf accepted
SharpeRatio(RaRf_xts$Ra,RaRF_xts$Rf)
Ra
#StdDev Sharpe (Rf=0.2%, p=95%): 0.5956861
#VaR Sharpe (Rf=0.2%, p=95%): 0.6704678
#ES Sharpe (Rf=0.2%, p=95%): 0.4820222
The issue here is the second argument Rf
should be Rb
for tq_performance
. Try this:
RaRf %>%
tq_performance(Ra = Ra, Rb = Rf, performance_fun = SharpeRatio)
How does that work for functions that take both Rb and Rf arguments, like AppraisalRatio
?
It works by selecting the column names as your first two arguments and the performance analytics function as the third.
But we need 3 column names for AppraisalRatio
.
> args(AppraisalRatio)
#function (Ra, Rb, Rf = 0, method = c("appraisal", "modified",
# "alternative"), ...)
NULL
Excuse me if I'm being dense.
Normally the Rf is provided as a scalar. Are you suggesting we should also include an option for a column?
Exactly. Typically, I retrieve the risk-free rate as a series along with everything else. The Performance Analytics package takes it as a scalar OR a series so it's easy to pass them all along. That said, it's no big deal to convert the series to a scalar, like so:
Rf_scalar <-Rf_series %>% tq_performance(Ra=Rf_series, performance_fun = Return.annualized) %>%
as.numeric()/12 #assuming we want monthly return.
As a practical issue, the fact that we've had very stable short-term rates for years means it doesn't really matter. Indeed, those rates have been about zero so just omitting Rf works too. High and volatile short-term rates might produce different results between a scalar and a vector for some of these stats.