New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative to error when periodicity() called on one observation #230

Closed
joshuaulrich opened this Issue Mar 21, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@joshuaulrich
Owner

joshuaulrich commented Mar 21, 2018

There have been a few instances where it has caused issues and/or confusion when periodicity() throws an error when called on an object with only one observation (e.g. #155, #161, rstudio/dygraphs#66, braverock/PerformanceAnalytics#92).

Is there a better alternative to an error? Perhaps return NA, with a warning? Or return an "empty" periodicity object, with a warning? An "empty" periodicity object could look something like what I recommended JJ implement for dygraphs:

list(difftime = structure(0, units = "secs", class = "difftime"),
  frequency = 0,
  start = start(data),
  end = end(data),
  units = "secs",
  scale = "seconds",
  label = "second"),
class = "periodicity")
@braverock

This comment has been minimized.

Show comment
Hide comment
@braverock

braverock Mar 21, 2018

Contributor

I think I'd prefer returning an empty periodicity argument.

Contributor

braverock commented Mar 21, 2018

I think I'd prefer returning an empty periodicity argument.

@JJHerranzs

This comment has been minimized.

Show comment
Hide comment
@JJHerranzs

JJHerranzs Apr 28, 2018

Dear Joshua, I have just received the following error "Error in periodicity(R): can not calculate periodicity of 1 observation" and I am stuck. The point of this code is to run a backtest on the performance of an optimised portfolio. Is there any way you know how to go about this situation?

Here is my code:

library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(stockPortfolio)
library(ROI)
library(ROI.plugin.glpk)
library(ROI.plugin.quadprog)
library(portfolio)
library(zoo)
library(MarkowitzR)

# Get data
asset.names <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
getSymbols(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"), freq = "daily", from = "2013-01-01", to = "2017-12-31")
na.omit(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"),cols=default, invert=FALSE)

# Assign to dataframe
# Get adjusted prices
prices.data <- merge.zoo(IUSA.L[,6], CUSS.L[,6], IWRD.L[,6], IEEM.L[,6], LQDE.L[,6], ITPS.L[,6], SHYU.L[,6], IWDP.L[,6], SGLN.L[,6])

# Calculate returns
returns.data <- CalculateReturns(prices.data)
returns.data <- na.omit(returns.data)

# Set names
colnames(returns.data) <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
funds <- colnames(returns.data)

# Create initial portfolio
init <- portfolio.spec(assets=funds)
init <- add.constraint(portfolio=init, type="full_investment")

# Create minimun variance portfolio
minvar.portf <- add.objective(portfolio=init, type="risk", name="var")

# Create mean-variance portfolio
meanvar.portf <- add.objective(portfolio=minvar.portf, type="return", name="mean")
opt_meanvar <- optimize.portfolio(R=returns.data, portfolio = meanvar.portf, optimize_method = "ROI")
print(opt_meanvar)
summary(opt_meanvar)
extractStats(opt_meanvar)

backtesting <- optimize.portfolio.rebalancing(R=returns.data, portfolio = meanvar.portf, optimize_method = "ROI", rebalance_on="quarters", training_period = 60)
print(backtesting)

Thanks!

JJHerranzs commented Apr 28, 2018

Dear Joshua, I have just received the following error "Error in periodicity(R): can not calculate periodicity of 1 observation" and I am stuck. The point of this code is to run a backtest on the performance of an optimised portfolio. Is there any way you know how to go about this situation?

Here is my code:

library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(stockPortfolio)
library(ROI)
library(ROI.plugin.glpk)
library(ROI.plugin.quadprog)
library(portfolio)
library(zoo)
library(MarkowitzR)

# Get data
asset.names <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
getSymbols(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"), freq = "daily", from = "2013-01-01", to = "2017-12-31")
na.omit(c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L"),cols=default, invert=FALSE)

# Assign to dataframe
# Get adjusted prices
prices.data <- merge.zoo(IUSA.L[,6], CUSS.L[,6], IWRD.L[,6], IEEM.L[,6], LQDE.L[,6], ITPS.L[,6], SHYU.L[,6], IWDP.L[,6], SGLN.L[,6])

# Calculate returns
returns.data <- CalculateReturns(prices.data)
returns.data <- na.omit(returns.data)

# Set names
colnames(returns.data) <- c("IUSA.L", "CUSS.L", "IWRD.L", "IEEM.L", "LQDE.L", "SHYU.L", "ITPS.L", "IWDP.L", "SGLN.L")
funds <- colnames(returns.data)

# Create initial portfolio
init <- portfolio.spec(assets=funds)
init <- add.constraint(portfolio=init, type="full_investment")

# Create minimun variance portfolio
minvar.portf <- add.objective(portfolio=init, type="risk", name="var")

# Create mean-variance portfolio
meanvar.portf <- add.objective(portfolio=minvar.portf, type="return", name="mean")
opt_meanvar <- optimize.portfolio(R=returns.data, portfolio = meanvar.portf, optimize_method = "ROI")
print(opt_meanvar)
summary(opt_meanvar)
extractStats(opt_meanvar)

backtesting <- optimize.portfolio.rebalancing(R=returns.data, portfolio = meanvar.portf, optimize_method = "ROI", rebalance_on="quarters", training_period = 60)
print(backtesting)

Thanks!

@joshuaulrich

This comment has been minimized.

Show comment
Hide comment
@joshuaulrich

joshuaulrich Apr 28, 2018

Owner

@JJHerranzs this is meant to be an issue and feature request tracker for xts, not a support forum. The contributing guide points to StackOverflow and the R-SIG-Finance as places to ask general questions about how to use the software.

The error is likely because one of your rebalancing periods only contains a single observation. But please take your question to another forum. I would prefer comments on this issue stay on topic.

Owner

joshuaulrich commented Apr 28, 2018

@JJHerranzs this is meant to be an issue and feature request tracker for xts, not a support forum. The contributing guide points to StackOverflow and the R-SIG-Finance as places to ask general questions about how to use the software.

The error is likely because one of your rebalancing periods only contains a single observation. But please take your question to another forum. I would prefer comments on this issue stay on topic.

Repository owner deleted a comment from JJHerranzs Apr 28, 2018

@JJHerranzs

This comment has been minimized.

Show comment
Hide comment
@JJHerranzs

JJHerranzs Apr 28, 2018

I apologise for this and thank you very much for your answer, I am still a beginner in R so I am not sure how I am going to solve it but I will have a go!

JJHerranzs commented Apr 28, 2018

I apologise for this and thank you very much for your answer, I am still a beginner in R so I am not sure how I am going to solve it but I will have a go!

@braverock

This comment has been minimized.

Show comment
Hide comment
@braverock

braverock Apr 28, 2018

Contributor

as @joshuaulrich pointed out, this isn't a support forum, but I'll give you a few other hints about where your problem might be. Your problem is most likely in how you are using PortfolioAnalytics.

You are specifying rebalancing on quarters. Typically you would do this using daily or weekly or monthly returns input data. See, for example

data('edhec')

for a monthly return series.

You've further specified a training period of 60 periods (quarters). That would be 15 years of data before you generated your first portfolio and started rebalancing. Is this correct? Your first symbol: IUSA.L , looks as though it may only have been listed in 2002.

If you're still having problems, I suggest posting to Stack Overflow or R-SIG-Finance
R-SIG-Finance

Contributor

braverock commented Apr 28, 2018

as @joshuaulrich pointed out, this isn't a support forum, but I'll give you a few other hints about where your problem might be. Your problem is most likely in how you are using PortfolioAnalytics.

You are specifying rebalancing on quarters. Typically you would do this using daily or weekly or monthly returns input data. See, for example

data('edhec')

for a monthly return series.

You've further specified a training period of 60 periods (quarters). That would be 15 years of data before you generated your first portfolio and started rebalancing. Is this correct? Your first symbol: IUSA.L , looks as though it may only have been listed in 2002.

If you're still having problems, I suggest posting to Stack Overflow or R-SIG-Finance
R-SIG-Finance

@joshuaulrich joshuaulrich added this to the Release 0.11-0 milestone Jul 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment