Hi all,
I am trying to define a program and find parameters (para_a, para_b) optimizing an optimizing function. What I want to do is minimize the difference between beta dist. CDF and empirically observed CDF. Here is my code.
1) defining a program
2) I want to maximize -`total_sum_diff' stored in local lnf. Conditions for para_a and para_b are described below, and initial values of para_a and para_b are also set.
As myobjfunc is ended, a msg (could not find feasible values) pops up. Does it just mean that ml command cannot find optimal values? Or is there anything wrong with my code?
I am trying to define a program and find parameters (para_a, para_b) optimizing an optimizing function. What I want to do is minimize the difference between beta dist. CDF and empirically observed CDF. Here is my code.
1) defining a program
Code:
program define myobjfunc, rclass set trace on args lnf para_a para_b tempvar observed_cdf diff sum_diff temp_cdf current_sum_diff local xl . local xr . local lower_bound = round(assigned_value_lower, 0.01) local upper_bound = round(assigned_value_upper, 0.01) local total_sum_obsCDF 0 local total_sum_diff 0 local pred_cdf 0 forvalues i = 1/10 { gen temp_value = qsp7dens_`i'_new quietly sum temp_value local this_sum = r(sum) local total_sum_obsCDF = `total_sum_obsCDF' + `this_sum' drop temp_value if `i' == 10 { local xl -0.16 local xr -0.12 } else if `i' == 9 { local xl -0.12 local xr -0.08 } else if `i' == 8 { local xl -0.08 local xr -0.04 } else if `i' == 7 { local xl -0.04 local xr -0.02 } else if `i' == 6 { local xl -0.02 local xr 0.00 } else if `i' == 5 { local xl 0.00 local xr 0.02 } else if `i' == 4 { local xl 0.02 local xr 0.04 } else if `i' == 3 { local xl 0.04 local xr 0.08 } else if `i' == 2{ local xl 0.08 local xr 0.12 } else if `i' == 1 { local xl 0.12 local xr 0.16 } di "xl is " `xl', "xr is "`xr' if (`xr' > `upper_bound') { local temp_cdf = 1 } else if (`lower_bound' <= `xl' & `xl' < `upper_bound' & `lower_bound' < `xr' & `xr' <= `upper_bound') { local temp_cdf = ibeta(`para_a', `para_b', (`xr' - `lower_bound') / (`upper_bound' - `lower_bound')) } else if (`xl' < `lower_bound') { local temp_cdf = 0 } local pred_cdf = `temp_cdf' local diff = (`total_sum_obsCDF' - `pred_cdf')^2 local total_sum_diff = `total_sum_diff' + `diff' } local lnf = -`total_sum_diff' end
Code:
constraint 1 para_a > 1 constraint 2 para_b > 1 levelsof uniqueid, local(userids) foreach id of local userids{ preserve // I want to loop id-by-id so generate variable `is_current_user'. For current user, the ml is operated. gen is_current_user = (uniqueid == `id') keep if is_current_user & No_dens>= 3 // ml ml model lf myobjfunc (para_a:, freeparm) (para_b:, freeparm), constraints(1 2) ml init /para_a=1.04 /para_b=1.5 ml maximize ml check // store optimal values of para_a and para_b in original (.dta) dataset. matrix opt_values = e(b) scalar opt_a_temp = opt_values[1,1] scalar opt_b_temp = opt_values[1,2] // opt_a and opt_b: variables defined in .dta replace opt_a = opt_a_temp if is_current_user replace opt_b = opt_b_temp if is_current_user restore }