Optimization Action Set
Johnson’s Systems of Distributions as a Black-Box Objective
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 3, Shared Concepts (SAS Optimization: Mathematical Optimization Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
For this example, a data set that contains randomly generated observations is used to estimate the parameters for the Johnson
family of distributions (Bowman and Shenton 1983). The objective is the log likelihood for the family, which involves four variables,
,
where
Here, d represents the randomly generated observations. Therefore, denotes the value of d in the kth observation of the data table that is generated.
The following code demonstrates how to create the data table of random values, promote the data table so that it is visible to all the parallel sessions that are used by the solveBlackbox action, and then actually call the solveBlackbox action to solve the optimization problem. This example assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.sudata;
n = 20000;
theta = -1;
sigma = 1;
delta = 3;
gamma = 5;
rngSeed=12345;
call streaminit(rngSeed);
do i = 1 to n;
z = rand('NORMAL');
a = exp( (z - gamma)/delta );
d = sigma * ( (a**2 - 1)/(2*a) ) + theta;
output;
end;
keep d;
run;
proc cas noqueue;
promote / name="sudata";
/* The CASL code for evaluating the objective */
source caslEval;
fetch result=res / table={name="sudata"} to=20000 maxRows=20000;
tab = findtable(res);
n = tab.nrows;
fx = 0;
do k=1 to n;
yk = (tab[k,"d"] - x1) / x2;
zk = yk + sqrt(1 + yk*yk);
fx = fx + ((x3 + x4*log(zk))*(x3 + x4*log(zk)) + log(1+yk*yk));
end;
fx = fx * (-0.5);
fx = fx + n*log(x4) - n*log(x2);
f['obj'] = fx;
send_response(f);
endsource;
/* Invoke the solveBlackbox action */
optimization.solveBlackbox /
decVars = {
{name='x1'},
{name='x2', lb=1e-12}
{name='x3'},
{name='x4', lb=1e-12}
},
obj = {{name='obj', type='max'}},
func = {eval=caslEval},
nParallel = 8,
primalOut={name="p_out", replace=true}
;
run; quit;
proc print data=mycas.p_out; run;
Output 2.8.1 shows the output from running the preceding code.
Output 2.8.1: Johnson's Systems of Distributions as a Black-Box Objective
| Problem Summary | |
|---|---|
| Problem Type | NLP |
| Number of Variables | 4 |
| Continuous Variables | 4 |
| Integer Variables | 0 |
| Number of Constraints | 0 |
| Linear Constraints | 0 |
| Nonlinear Constraints | 0 |
| Number of Objectives | 1 |
| Objective Sense | Maximize |
| Option Summary | |
|---|---|
| Convergence Tolerance | 1E-6 |
| Cache Max Size | 250000 |
| Cache Tolerance | 1E-9 |
| Feasibility Tolerance | 0.001 |
| Max Func Evaluations | 380000 |
| Max Iterations | 10 |
| Max Time | 1.797693E308 |
| Num Global Solvers | 1 |
| Num Local Solvers | 8 |
| Population Size | 30 |
| Seed | 1 |
| Log Frequency | 1 |
| Log Level | 1 |
| Solution Summary | |
|---|---|
| Solution Status | Generations complete |
| Objective | -11367.29935 |
| Infeasibility | 0 |
| Iterations | 10 |
| Evaluations | 295 |
| Cached Evaluations | 6 |
| Obs | _tag_ | x1 | x2 | x3 | x4 | obj | _inf_ | _iter_ | _evalTime_ |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 282 | 43.5827 | 46.4514 | 43.5879 | 48.8352 | -11367.30 | 0 | 10 | 1.87707 |
Johnson’s Systems of Distributions as a Black-Box Objective
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
For this example, a data set that contains randomly generated observations is used to estimate the parameters for the Johnson
family of distributions (Bowman and Shenton 1983). The objective is the log likelihood for the family, which involves four variables,
,
where
Here, d represents the randomly generated observations. Therefore, denotes the value of d in the kth observation of the data table that is generated.
In order to run the code in this example, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the sudata data to a comma-separated-value (CSV) file sudata.csv.
The following code loads the necessary data from the CSV file sudata.csv into a CAS table, promotes the table so that it is visible to all the parallel sessions that are used by the solveBlackbox action, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
s:loadTable{caslib="CASUSER", path="sudata.csv"}
s:promote{name="sudata"}
-- The CASL code for evaluating the objective
caslEval = [[
fetch result=res / table={name="sudata"} to=20000 maxRows=20000;
tab = findtable(res);
n = tab.nrows;
fx = 0;
do k=1 to n;
yk = (tab[k,"d"] - x1) / x2;
zk = yk + sqrt(1 + yk*yk);
fx = fx + ((x3 + x4*log(zk))*(x3 + x4*log(zk)) + log(1+yk*yk));
end;
fx = fx * (-0.5);
fx = fx + n*log(x4) - n*log(x2);
f['obj'] = fx;
send_response(f);
]]
-- Invoke the solveBlackbox action
s:optimization_solveBlackbox {
decVars = {
{name='x1'},
{name='x2', lb=1e-12},
{name='x3'},
{name='x4', lb=1e-12}
},
obj = {
{name='obj', type='max'}
},
func = {eval=caslEval},
nParallel = 8,
primalOut = {name='p_out', replace=true}
}
Johnson’s Systems of Distributions as a Black-Box Objective
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
For this example, a data set that contains randomly generated observations is used to estimate the parameters for the Johnson
family of distributions (Bowman and Shenton 1983). The objective is the log likelihood for the family, which involves four variables,
,
where
Here, d represents the randomly generated observations. Therefore, denotes the value of d in the kth observation of the data table that is generated.
The following code loads the optimization action set, creates a data frame for the random data and uploads it to a CAS table, promotes the table so that it is visible to all the parallel sessions that are used by the solveBlackbox action, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
s.loadactionset('optimization')
# Create a data frame for the random data used in this problem
# and upload the data frame as a CAS table named 'sudata'.
n = 20000;
theta = -1;
sigma = 1;
delta = 3;
gamma = 5;
df = pd.DataFrame(np.random.randn(n,1), columns=['z']);
df['a'] = np.exp((df['z']-gamma)/delta);
df['d'] = sigma * ((df['a']*df['a']-1)/(2*df['a'])) + theta;
df = df.drop(['z', 'a'], axis=1)
s.upload_frame(df, casout=dict(name='sudata', replace=True));
# Promote the CAS data table so it's accessible to all
# subsessions created by the solveBlackbox action
s.promote(table='sudata')
# The CASL code for evaluating the objective
caslEval='''
fetch result=res / table={name="sudata"} to=20000 maxRows=20000;
tab = findtable(res);
n = tab.nrows;
fx = 0;
do k=1 to n;
yk = (tab[k,"d"] - x1) / x2;
zk = yk + sqrt(1 + yk*yk);
fx = fx + ((x3 + x4*log(zk))*(x3 + x4*log(zk)) + log(1+yk*yk));
end;
fx = fx * (-0.5);
fx = fx + n*log(x4) - n*log(x2);
f['obj'] = fx;
send_response(f);
''';
# Invoke the solveBlackbox action
s.optimization.solveBlackBox(
decVars = [
dict(name='x1'),
dict(name='x2', lb=1e-12),
dict(name='x3'),
dict(name='x4', lb=1e-12)
],
obj = [dict(name='obj', type='max')],
func = dict(eval=caslEval),
nParallel=8,
primalOut=dict(name="p_out", replace=True)
);
Johnson’s Systems of Distributions as a Black-Box Objective
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
For this example, a data set that contains randomly generated observations is used to estimate the parameters for the Johnson
family of distributions (Bowman and Shenton 1983). The objective is the log likelihood for the family, which involves four variables,
,
where
Here, d represents the randomly generated observations. Therefore, denotes the value of d in the kth observation of the data table that is generated.
In order to run the code in this example, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the sudata data to a comma-separated-value (CSV) file sudata.csv.
The following code loads the necessary data from the CSV file sudata.csv into a CAS table, promotes the table so that it is visible to all the parallel sessions that are used by the solveBlackbox action, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
# Load data table from CSV file
cas.table.loadTable(s, caslib='CASUSER', path='sudata.csv', promote='yes')
# The CASL code for evaluating the objective
caslEval = "
fetch result=res / table={name='sudata'} to=20000 maxRows=20000;
tab = findtable(res);
n = tab.nrows;
fx = 0;
do k=1 to n;
yk = (tab[k,'d'] - x1) / x2;
zk = yk + sqrt(1 + yk*yk);
fx = fx + ((x3 + x4*log(zk))*(x3 + x4*log(zk)) + log(1+yk*yk));
end;
fx = fx * (-0.5);
fx = fx + n*log(x4) - n*log(x2);
f['obj'] = fx;
send_response(f);
"
# Invoke the solveBlackbox action
cas.optimization.solveBlackbox (s,
decVars = list(
list(name='x1'),
list(name='x2', lb=1e-12),
list(name='x3'),
list(name='x4', lb=1e-12)
),
obj = list(
list(name='obj', type='max')
),
func = list(eval=caslEval),
nParallel = 8,
primalOut = list(name='p_out', replace='true')
)