proc iml;
start use_file(dataset, rettot);
file log;
put "reading file " dataset;
call execute('use ',dataset,';');
read all var _num_ into rettot;
call execute ('close ',dataset,';');
finish;
call use_file('_targr',targr);
call use_file('_covmat1',cov);
nrow = nrow(cov);
/* create start weight as the inverse of vol */
x0 = vecdiag(diag(cov)##(0.5));
x0 = 1/x0`;
/* define the objective function */
start func(x) global(cov, targr);
margr = x` # (cov * x`); /* marginal risk */
totr = margr[+]; /* total risk */
rcon = margr / totr;
f = (rcon - targr)##2 [,+];
f = f`[,+];
return(f);
finish func;
opt = j(1,11,.);
opt[1] = 0; /*0 = Minimize, 1 = Maximize*/
opt[10] = 0 /*number of nonlinear constraints*/;
blc = J(1, nrow, 0); /*Lower bound constraint*/
tc = j(1, 13, .);
/* first run nlpnra; if not optimal, then run nlpqn; if still not optimal then error */
call nlpnra(rc, x1, "func", x0, opt, blc, tc);
objv1 = func(x1);
x = x1;
error = 0;
if objv1 > 0.0001 then do;
call nlpqn(rc, x2, "func", x0, opt, blc, tc);
objv2 = func(x2);
if objv2 > objv1 then do; x = x1; error = 1; end;
else if objv2 > 0.0001 then do; x = x2; error = 1; end;
else do; x = x2; error = 0; end;
end;
print error; print objv1; print x; Hi all, I'm using nonlinear optimization procedure (nlpqn or nlpnra) to solve a set of non-negative weights. I have encountered the problem that the optimation is "successfully" completed but with suboptimal weights (by comparing with solutions obtained from excel solver). Mostly the suboptimal results will have one or two weights assigned as 0 (i.e., lower bounds). The log says "ABSGCONV convergence criterion satisfied." When I switch the procedure (nlpgn or nlpnra), I may or may not get the optimal weights. Is there a way that I can change the input parameters (such as the termination criteria) so that optimal weights will be solved? Thanks, Kun
... View more