Subscribe Bookmark RSS Feed

Defining parameters in a loop

akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

Hi,

I am using the following code to define formula in the column "Response Prediction Formula" in my table. As can be seen in the code, a parameter called "tuning" is defined in the formula. Now, instead of 1 parameter, I have to define k parameters in the formula such as tuning1, tuning2, ...tuningk. Is there a way I can do this using a for loop? Please let me know. Thanks!!!

 

Eval(
	Substitute(
			Expr(
				:Response Prediction Formula << set formula(tuning=1,b0=1,b1=1; b0+b1*f11)
				:Response Prediction Formula << setFormula( Parameter( {tuning = initialValueTBD,b0 = 0,b1 = 1}, b0+b1*f11 ) ),

			),
			Expr(initialValueTBD), 1,
			Expr(f11), evalexpr(f11) 
		)
	);

 

1 ACCEPTED SOLUTION

Accepted Solutions
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Here is some example code that uses a loop to creeate the list expression of parameters, which is then inserted into the expression to set the formula:

// build a list of tuning parameters
lstInitialTuningValues = {1,1,1,2,2};  // for example, 5 tuning params with these initial values
lstParams = {};
For (i=1,i<=NItems(lstInitialTuningValues),i++,
	Eval(Parse(EvalInsert("\[ 
		value = lstInitialTuningValues[i];
		InsertInto(lstParams, Expr( tuning^i^=^value^ ) )
	]\")));
);

show(lstParams);  // verify the tuning params are defined correctly

// add the parameters b0,b1
InsertInto(lstParams,Expr(b0=1));
InsertInto(lstParams,Expr(b1=1));

show(lstParams);  // final list of parameters

// set the formula ...
fml = "b0 + b1*f11";
str = Eval(Parse(EvalInsert("\[ 
	:Response Prediction Formula << Set Formula( Parameter( ^lstParams^ , ^fml^ ) )
]\")));

 This is the outcome:

parameqtn.PNG

-Dave
2 REPLIES
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Here is some example code that uses a loop to creeate the list expression of parameters, which is then inserted into the expression to set the formula:

// build a list of tuning parameters
lstInitialTuningValues = {1,1,1,2,2};  // for example, 5 tuning params with these initial values
lstParams = {};
For (i=1,i<=NItems(lstInitialTuningValues),i++,
	Eval(Parse(EvalInsert("\[ 
		value = lstInitialTuningValues[i];
		InsertInto(lstParams, Expr( tuning^i^=^value^ ) )
	]\")));
);

show(lstParams);  // verify the tuning params are defined correctly

// add the parameters b0,b1
InsertInto(lstParams,Expr(b0=1));
InsertInto(lstParams,Expr(b1=1));

show(lstParams);  // final list of parameters

// set the formula ...
fml = "b0 + b1*f11";
str = Eval(Parse(EvalInsert("\[ 
	:Response Prediction Formula << Set Formula( Parameter( ^lstParams^ , ^fml^ ) )
]\")));

 This is the outcome:

parameqtn.PNG

-Dave
akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

That works!! Thanks David!! :)