投资组合优化R - 错误

时间:2016-03-21 02:27:22

标签: r optimization finance portfolio

对不起,如果这是一个愚蠢的问题,但我现在试图解决这个问题3天了。每次我尝试运行投资组合优化时都会收到此错误,但无法弄清楚。

Error in assign(".objectivestorage", list(), envir = as.environment(.storage)) : 
  object '.storage' not found

我通常也会在第二和第三个目标中收到此警告:

In addition: Warning message:
In is.na(le) : is.na() applied to non-(list or vector) of type 'NULL'

这是我的代码:

##Import Dataset
setwd("D:\\Dropbox\\FUND - SSIF\\Portfolio Analysis Package")
Stocktrak<- Return.read("SSIF_Data.csv", frequency = "d")
# Create Objects for data and column names

R <- Stocktrak[, 1:17]
colnames(returns) <- c("JEC", "BNS", "AAPL", "PEG", "SLB", "TSM", "HD",
  "MON", "GWO", "TOT", "XPH", "CVS", "UNP", "KORS", "GNTX", "NWC", "WFC")
funds <- colnames(R)

# Create an initial portfolio object with leverage and box constraints
init <- portfolio.spec(assets=funds)
init <- add.constraint(portfolio=init, type="leverage", min_sum=0.99, max_sum=1.01)
init <- add.constraint(portfolio=init, type="box", min=0.01, max=0.65)

# Create Objectives for eq_meanETL Portfolio Optimization
eq_meanETL <- add.objective(portfolio=init, type="return", name="mean")
eq_meanETL <- add.objective(portfolio=eq_meanETL, type="risk", name="ETL", arguments=list(p=0.95))
eq_meanETL <- add.objective(portfolio=eq_meanETL, type="risk_budget", name="ETL", min_concentration=TRUE, arguments=list(p=0.95))

# Optimize Portfolio
opt_eq_meanETL <- optimize.portfolio(R=R, portfolio=eq_meanETL, optimize_method="DEoptim", search_size=2000, trace=TRUE, traceDE=5)

2 个答案:

答案 0 :(得分:2)

public class CustomAuthorize : AuthorizeAttribute { public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) { if (Authorize(actionContext)) { return; }else { HandleUnauthorizedRequest(actionContext); } } protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) { actionContext.Response = new HttpResponseMessage { StatusCode = (HttpStatusCode)401, Content = new StringContent("No tiene autorización para realizar esta acción") }; } private bool Authorize(System.Web.Http.Controllers.HttpActionContext actionContext) { try { var user = actionContext.RequestContext.Principal.Identity.Name; //boolean logic to determine if you are authorized. //We check for a valid token in the request header or cookie. if (user.Contains("luis")) return true; else return false; } catch (Exception) { return false; } } }

之前放置以下内容
optimize.portfolio

答案 1 :(得分:-1)

编辑

更改trace = FALSE

它会起作用。第一次来这里。

优化投资组合

opt_eq_meanETL&lt; - optimize.portfolio(R = R,portfolio = eq_meanETL,optimize_method =“DEoptim”,search_size = 2000,trace = FALSE,traceDE = 5)