Hello,
I am a graduate student (in ecology, so not a great background in mathematics or coding languages) attempting an optimization for a part of my thesis. Currently I have the coding below in an attempt to get the "program" to work with pseudo-data. This has been constructed based on publications, which use other programming languages, but the functions are "...local minimizer for smooth non-linear functions subject to bound-constrained parameters, and uses a quasi-newton method". Thus I believe the correct call is NLPQN, however when running this I do not get the expected optimization.
Proc IML;
start qfasa(y);
prey={17 41 5, 40 30 25}; /*pseudo-profile of three prey items with 2 variables each*/
predator={13.4, 29.5}; /*pseudo-profile of one predator with 2 variables that are the proportional addition of the prey items*/
predic=(prey#y);
diet=predic[,+];
KL1=((predator-diet)#log10(predator/diet)); /*Kulback-Liebler Distance*/
KL=KL1[+,];
Return(KL); /*currently must be a scalar, possible to get this for each predator column when more are added? (do loops?)*/
finish qfasa;
y={.5 .25 .25}; /* Initial guess at proportions*/
c={0 0 0, 1 1 1}; /*Constraints on parameters*/
optn={0 2};
call nlpqn(rc,xr,"qfasa",y,optn,c) ;
run;
*/Answer should be { .25 .15 .60}*/
Thanks for any help!
From your starting conditions and your answer, I'm guessing that you expect that the parameters sum to one? (Are they proportions?). You have not constrained the parameter sum.
There are two ways you can do this:
1) Leave the constraint matrix as it is and add a penalty term to the objective function. For example, add
return( KL + 1000*(sum(y)-1)##2)
2) Incorporate the constraints into the matrix:
c={0 0 0 . ., /* 0 <= p1, p2, p3 <=1 */
1 1 1 . .,
1 1 1 0 1}; /* p1 + p2 + p3 = 1 */
See SAS/IML(R) 12.1 User's Guide for specifying constraints.
From your starting conditions and your answer, I'm guessing that you expect that the parameters sum to one? (Are they proportions?). You have not constrained the parameter sum.
There are two ways you can do this:
1) Leave the constraint matrix as it is and add a penalty term to the objective function. For example, add
return( KL + 1000*(sum(y)-1)##2)
2) Incorporate the constraints into the matrix:
c={0 0 0 . ., /* 0 <= p1, p2, p3 <=1 */
1 1 1 . .,
1 1 1 0 1}; /* p1 + p2 + p3 = 1 */
See SAS/IML(R) 12.1 User's Guide for specifying constraints.
Thanks Rick, the constraints issue got it to do what I wanted!
Could you (anyone) give me some starting points on getting this to work column-wise on a dataset? Specifically, I have been trying to get column (sample) specific parameter estimates for the "predator" matrix.
I think I would need to see sample data to understand what you mean. It seems like the optimization requires a vector.
If you want to apply this optimization to several predator vectors that are stored in a data base, use the GLOBAL statement so that the predator vector is considered a parameter in the problem:
start qfasa(y) global(predator);
...
finish;
do i = 1 to ncol(big_matrix);
predator = big_matrix[, i];
call nlpqn(rc,xr,"qfasa",y,optn,c) ; /* solve for this column */
/* store or do something with xr */
end;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.