Prev 
Next 
Index-> 
contents 
reference 
index 
search 
external 
 
 
Up-> 
dismod_at 
user_example 
user_warm_start.py 
 
 
 
 
user_warm_start.py 
 
This is dismod_at-20221105 documentation:  Here is a link to its
current documentation Continuing a Fit Using Ipopt Warm Start Option Table 
Fixed Trace Table 
Source Code 
Option Table max_num_iter_fixed  = 5 warn_on_stderr  = false print_level_fixed  = 5 run this example Fixed Trace Table trace_fixed_table Source Code 
# values used to simulate data =  0.01 
# ------------------------------------------------------------------------ import import import =  'example/user/warm_start.py' 
if . argv[ 0 ]  !=  test_program  or len ( sys. argv)  !=  1  : 
   usage  =  'python3 '  +  test_program +  '\n' 
   usage +=  'where python3 is the python 3 program on your system\n' 
   usage +=  'and working directory is the dismod_at distribution directory\n' 
   sys. exit ( usage) 
print ( test_program) 
# # import dismod_at =  os. getcwd ()  +  '/python' 
if (  os. path. isdir (  local_dir +  '/dismod_at'  )  )  : 
   sys. path. insert ( 0 ,  local_dir) 
import # # change into the build/example/user directory if not . path. exists ( 'build/example/user' )  : 
   os. makedirs ( 'build/example/user' ) 
os. chdir ( 'build/example/user' ) 
# ------------------------------------------------------------------------ # Note that the a, t values are not used for this example def example_db ( file_name)  : 
   # note that the a, t values are not used for this case def fun_iota ( a,  t)  : 
      return ( 'prior_iota' ,  None,  None) 
   def fun_chi ( a,  t)  : 
      return ( chi_true,  None,  None) 
   # ---------------------------------------------------------------------- # age table: =  [  0.0 ,  5.0 ,  15.0 ,  35.0 ,  50.0 ,  75.0 ,  90.0 ,  100.0  ] 
   # # time table: =  [  1990.0 ,  2000.0 ,  2010.0 ,  2200.0  ] 
   # # integrand table: =  [ 
       {  'name' : 'Sincidence'  } 
   ] 
   # # node table: =  [  {  'name' : 'world' ,  'parent' : ''  }  ] 
   # # weight table: =  list () 
   # # covariate table: =  list () 
   # # mulcov table: =  list () 
   # # avgint table: empty =  list () 
   # # nslist_table: =  dict () 
   # ---------------------------------------------------------------------- # data table: =  list () 
   # values that are the same for all data rows =  { 
      'integrand' :    'Sincidence' , 
      'meas_value' :   iota_true, 
      'density' :      'gaussian' , 
      'meas_std' :     iota_true /  10 ., 
      'weight' :       '' , 
      'hold_out' :      False, 
      'age_lower' :     50.0 , 
      'age_upper' :     50.0 , 
      'time_lower' :    2000 ., 
      'time_upper' :    2000 ., 
      'node' :          'world' , 
      'subgroup' :      'world' , 
   } 
   data_table. append (  copy. copy ( row)  ) 
   # # ---------------------------------------------------------------------- # prior_table =  [ 
      {  # prior_iota 'name' :      'prior_iota' , 
         'density' :   'uniform' , 
         'lower' :     iota_true /  10 ., 
         'upper' :     iota_true *  10 ., 
         'mean' :      iota_true *  2.0 , 
      } 
   ] 
   # ---------------------------------------------------------------------- # smooth table =  'smooth_iota' 
   fun            =  fun_iota
   smooth_table =  [ 
      {   'name' : name, 
         'age_id' :[ 0 ], 
         'time_id' :[ 0 ], 
         'fun' : fun
      } 
   ] 
   # ---------------------------------------------------------------------- # rate table: =  [ 
      {   'name' :           'iota' , 
         'parent_smooth' :  'smooth_iota' , 
      } 
   ] 
   # ---------------------------------------------------------------------- # option_table =  [ 
      {  'name' : 'rate_case' ,               'value' : 'iota_pos_rho_zero'    }, 
      {  'name' : 'parent_node_name' ,        'value' : 'world'                }, 
      {  'name' : 'warn_on_stderr' ,          'value' : 'false'                }, 
      {  'name' : 'quasi_fixed' ,             'value' : 'false'                }, 
      {  'name' : 'max_num_iter_fixed' ,      'value' : '5'                    }, 
      {  'name' : 'print_level_fixed' ,       'value' : '0'                    }, 
      {  'name' : 'tolerance_fixed' ,         'value' : '1e-8'                 }, 
      {  'name' : 'max_num_iter_random' ,     'value' : '50'                   }, 
      {  'name' : 'print_level_random' ,      'value' : '0'                    }, 
      {  'name' : 'tolerance_random' ,        'value' : '1e-10'                } 
   ] 
   # ---------------------------------------------------------------------- # subgroup_table =  [  {  'subgroup' : 'world' ,  'group' : 'world'  }  ] 
   # ---------------------------------------------------------------------- # create database . create_database ( 
      file_name, 
      age_list, 
      time_list, 
      integrand_table, 
      node_table, 
      subgroup_table, 
      weight_table, 
      covariate_table, 
      avgint_table, 
      data_table, 
      prior_table, 
      smooth_table, 
      nslist_table, 
      rate_table, 
      mulcov_table, 
      option_table
   ) 
   # ---------------------------------------------------------------------- return # =========================================================================== # Create database =  'example.db' 
example_db ( file_name) 
# # first fit command =  '../../devel/dismod_at' 
dismod_at. system_command_prc ([  program,  file_name,  'init'  ]) 
dismod_at. system_command_prc ([  program,  file_name,  'fit' ,  'fixed'  ]) 
# # trace_fixed table =  False
connection        =  dismod_at. create_connection ( file_name,  new) 
trace_fixed_table =  dismod_at. get_table_dict ( connection,  'trace_fixed' ) 
connection. close () 
# trace includes iteration zero assert (  len ( trace_fixed_table)  ==  6  ) 
# # warm start second fit . system_command_prc ( 
   [  program,  file_name,  'fit' ,  'fixed' ,  'warm_start' 
]) 
# ----------------------------------------------------------------------- # read database =  False
connection        =  dismod_at. create_connection ( file_name,  new) 
var_table         =  dismod_at. get_table_dict ( connection,  'var' ) 
rate_table        =  dismod_at. get_table_dict ( connection,  'rate' ) 
fit_var_table     =  dismod_at. get_table_dict ( connection,  'fit_var' ) 
log_table         =  dismod_at. get_table_dict ( connection,  'log'  ) 
trace_fixed_table =  dismod_at. get_table_dict ( connection,  'trace_fixed' ) 
connection. close () 
# # second fit should converge in 2 iteations assert (  len ( trace_fixed_table)  <=  3  ) 
# # check that we a warning (maximum number iterations during first fit) =  0 
for in : 
   if [ 'message_type' ]  ==  'warning'  : 
      warning_count +=  1 
assert in [  1 ,  2 ] 
# assert len ( var_table)  ==  1 
fit_value =  fit_var_table[ 0 ][ 'fit_var_value' ] 
var_row   =  var_table[ 0 ] 
rate_id   =  var_row[ 'rate_id' ] 
rate_name =  rate_table[ rate_id][ 'rate_name' ] 
assert ==  'iota' 
rel_err   =  fit_value /  iota_true -  1.0 
if abs (  rel_err )  >  1e-6  : 
   print (  "iota rel_err = " ,  rel_err) 
# ----------------------------------------------------------------------------- print ( 'warm_start.py: OK' ) 
# -----------------------------------------------------------------------------