Hello everyone,
Below is the Stata code that does exactly what I am looking for but the looping takes a very long time. Given the set up it may weeks to complete the entire 311,000 iterations.
Essentially what I am trying to achieve is to determine the value `x' that ensures that the average of YIndem (avg1) and average of WeathIndem (avg2) are similar within a reasonable tolerance level.
The is equivalent to "Goal seek" or "Solver" in excel but looping over will take weeks. I was hoping I can use Mata and optimize function solve such a problem.
This is my attempt using Mata and Optimize function
However, I get these errors
Any help or direction to any resource that can help me will be appreciated.
Below is the Stata code that does exactly what I am looking for but the looping takes a very long time. Given the set up it may weeks to complete the entire 311,000 iterations.
Essentially what I am trying to achieve is to determine the value `x' that ensures that the average of YIndem (avg1) and average of WeathIndem (avg2) are similar within a reasonable tolerance level.
The is equivalent to "Goal seek" or "Solver" in excel but looping over will take weeks. I was hoping I can use Mata and optimize function solve such a problem.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input double YieldIndex float SWCIndex double conv_factor .9253806198611291 .9500761 .6226526011135668 .7824689060953668 1.0973326 .6226526011135668 1.6404846532462987 1.2310717 .6226526011135668 1.135791047177306 1.0770227 .6226526011135668 .5527689833452102 .9240186 .6226526011135668 1.2941161617887758 1.0016987 .6226526011135668 .7024821439818003 .9564832 .6226526011135668 1.4052729869411842 1.1745064 .6226526011135668 1.316751439047902 1.1611431 .6226526011135668 1.2891894358766425 1.0278761 .6226526011135668 .9909411357309351 .8795984 .6226526011135668 1.3410032409929848 1.3822783 .6226526011135668 1.0861081686220508 .8990123 .6226526011135668 .6649476376589332 .8974741 .6226526011135668 .3341956330126147 .6080579 .6226526011135668 .909491036549925 .9555679 .6226526011135668 1.2837267687754703 1.0622913 .6226526011135668 .9087439508922139 1.1295747 .6226526011135668 .853106566143711 1.3886855 .6226526011135668 .4042373654867438 1.0403242 .6226526011135668 end
Code:
gen Ydeduct=. gen AvgYIndem=. gen AvgWIndem=. gen YIndem=. gen WeathIndem=. forval a=1/311619{ replace WeathIndem=max(0,min(1,(1-0.05-SWCIndex)*conv_factor)) quietly summarize WeathIndem local avg2 = r(mean) local x=0 local tol = 0.00001 local diff =10 while `diff'>`tol'{ replace YIndem = max(0,min(1,1-`x'-YieldIndex)) if quietly summarize YIndem local avg1 = r(mean) local diff=abs(`avg1' - `avg2') local x =`x' + 0.0001 di "`x'" di "`avg1'" di "`avg2'" di "`diff'" } replace Ydeduct=`x' replace AvgYIndem= `avg1' replace AvgWIndem=`avg2' }
This is my attempt using Mata and Optimize function
Code:
mata void myfunc(todo, x, lnf, g, H) { SWCIndex=st_data(., "SWCIndex") conv_factor=st_data(.,"conv_factor") YieldIndex=st_data(.,"YieldIndex") variable1=min((1, max((0, (0.95 :- SWCIndex) :* conv_factor)))) variable2=min((1, max((0, 1 :-x :-YieldIndex) ))) mean_v1=mean(variable1) mean_v2=mean(variable2) diff_sq=(mean_v1-mean_v2)^2 lnf=-diff_sq *st_numscalar("lnf",lnf) } S = optimize_init() optimize_init_which(S, "max") optimize_init_evaluator(S, &myfunc()) optimize_init_params(S, 0) bh = optimize(S) bh end
However, I get these errors
Code:
myfunc(): 3200 conformability error opt__calluser0_d(): - function returned error opt__d0_calluser(): - function returned error deriv__call1user_d(): - function returned error _deriv__compute_value(): - function returned error _deriv(): - function returned error opt__eval_nr_d0(): - function returned error opt__eval(): - function returned error opt__looputil_iter0_common(): - function returned error opt__looputil_iter0_nr(): - function returned error opt__loop_nr(): - function returned error opt__loop(): - function returned error optimize(): - function returned error <istmt>: - function returned error r(3200);