Skip to content
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

itermax does not accept a class numeric from variable #24

Open
OmarOmeiri opened this issue Jul 1, 2020 · 2 comments
Open

itermax does not accept a class numeric from variable #24

OmarOmeiri opened this issue Jul 1, 2020 · 2 comments

Comments

@OmarOmeiri
Copy link

can't seem to pass a value into the itermax argument.

This does not work

 itermax <- 50
 prtf <- optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = itermax,  traceDE=20, trace=T)

and this works just fine

prtf <- optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = 50, traceDE=20, trace=T)
I will build an API using this, so no, I cannot just put the number in there, it comes from outside the script.

I've tried to put it into a numeric vector and extract the number from it, tried to itermax = as.numeric(itermax)

but absolutely no success.

I get a non-numeric argument to binary operator error.

Error in search_size/itermax: non-numeric argument to binary operator
Traceback:

1. c(df_all_weights, max_sr_weights, ef_df, risk_return_plot, max_sr_rets, 
 .     all_assets_plot, non_zero_assets_plot) %<-% portfolio_build(returns, 
 .     group_max_alloc, group_indexes, search_size)
2. tryCatch(multi_assign(substitute(x), value, parent.frame()), 
 .     invalid_lhs = function(e) {
 .         stop("invalid `%<-%` left-hand side, ", e$message, call. = FALSE)
 .     }, invalid_rhs = function(e) {
 .         stop("invalid `%<-%` right-hand side, ", e$message, call. = FALSE)
 .     })
3. tryCatchList(expr, classes, parentenv, handlers)
4. tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), 
 .     names[nh], parentenv, handlers[[nh]])
5. doTryCatch(return(expr), name, parentenv, handler)
6. tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
7. tryCatchOne(expr, names, parentenv, handlers[[1L]])
8. doTryCatch(return(expr), name, parentenv, handler)
9. multi_assign(substitute(x), value, parent.frame())
10. portfolio_build(returns, group_max_alloc, group_indexes, search_size)
11. optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), 
  .     search_size = search_size, itermax = itermax, traceDE = 20, 
  .     trace = T)

What could possibly be the cause of this misinterpretation of the optimize function? Thanks!

@braverock
Copy link
Owner

this isn't a completely reproducible example, so I can't be sure. I can guess though.

Try setting a different variable for itermax.

n_itermax <- 50
optimize.portfolio(asset_returns, portfolio = base_pf, optimize_method = c("DEoptim"), search_size = search_size, itermax = n_itermax,  traceDE=20, trace=TRUE)

my guess is that you are experiencing a scoping and lazy evaluation problem, and by the time itermax is evaluated, it has been reset somewhere to a default, but if you use a different variable name, when it is evaluated it will always be your value.

@braverock
Copy link
Owner

if this doesn't work, please provide a self contained reproducible example, and we will try to debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants