# These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets. A_set = [l for i, l in enumerate(opt_model.meas_lambdas) if (i % 4 == 0)] # Finally we run the variance estimatator using the arguments shown in Seciton 4.3.3 results_variances = v_estimator.run_opt('ipopt', tee=True, solver_options=options, tolerance=1e-5, max_iter=15, subset_lambdas=A_set) # Variances can then be displayed print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.sigma_sq): print(k, v) # and the sigmas for the parameter estimation step are now known and fixed sigmas = results_variances.sigma_sq #========================================================================= # USER INPUT SECTION - PARAMETER ESTIMATION #========================================================================= # In order to run the paramter estimation we create a pyomo model as described in section 4.3.4
# given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 options['linear_solver'] = 'ma57' options['max_iter'] = 2000 # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets. init_sigmas = 1e-10 # This will provide a list of sigma values based on the different delta values evaluated. results_variances = v_estimator.run_opt('ipopt', method = 'alternate', tee=False, solver_opts=options, secant_point = 1e-11, initial_sigmas = init_sigmas) # Variances can then be displayed print("\nThe estimated variances are:\n") for k,v in six.iteritems(results_variances.sigma_sq): print(k,v) # and the sigmas for the parameter estimation step are now known and fixed # sigmas = sigmas sigmas = results_variances.sigma_sq #========================================================================= # USER INPUT SECTION - PARAMETER ESTIMATION
v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=4, ncp=1, scheme='LAGRANGE-RADAU') v_estimator.initialize_from_trajectory('Z', results_sim.Z) v_estimator.initialize_from_trajectory('S', results_sim.S) v_estimator.scale_variables_from_trajectory('Z', results_sim.Z) v_estimator.scale_variables_from_trajectory('S', results_sim.S) options = {} #{'mu_init': 1e-6, 'bound_push': 1e-6} results_variances = v_estimator.run_opt('ipopt', tee=True, solver_options=options, tolerance=1e-6) print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.sigma_sq): print(k, v) sigmas = results_variances.sigma_sq #results_variances.sigma_sq ################################################################################# builder = TemplateBuilder() builder.add_mixture_component(concentrations) builder.add_parameter('k', bounds=(0.0, 1.0)) builder.add_spectral_data(results_sim.D) builder.set_odes_rule(rule_odes)
# If we do not wish to use the Chen et al. way of solving for variances, there is a simpler # and often more reliable way of finding variances. This is done by first assuming we have no model # variance and solving for the overall variance. See details in the documentation under Section 4.16 # This value will give us our worst possible device variance (i.e. all variance is explained using # the device variance) # Once the worst case device variance we solve the maximum likelihood # problem with different values of sigma in order to converge upon the most likely # split between the total device variance and the total model variance, we can then, # based on this solution solve for the individual model variances if we wish. # To do this we only need to provide an initial value for the variances. init_sigmas = 1e-8 results_variances = v_estimator.run_opt('ipopt', method='alternate', tee=False, initial_sigmas=init_sigmas, solver_opts=options, tolerance=1e-10, secant_point=6e-8, individual_species=False) # other options that may needed include an option to provide the second point for # the secant method to obtain good starting values, as well as the the tolerance for # when the optimal solution is reached. Additionally, it may be useful to solve for the individual # species' variances in addition to the overall model variance. # Variances can then be displayed print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.sigma_sq): print(k, v) # and the sigmas for the parameter estimation step are now known and fixed
# While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets. A_set = [l for i,l in enumerate(opt_model.meas_lambdas) if (i % 4 == 0)] # Finally we run the variance estimatator using the arguments shown in Seciton 4.3.3 results_variances = v_estimator.run_opt('ipopt', report_time = True, lsq_ipopt = True, tee=True, fixed_device_variance = 3e-06, solver_opts=options, tolerance=1e-5, max_iter=15, subset_lambdas=A_set) # Variances can then be displayed print("\nThe estimated variances are:\n") for k,v in six.iteritems(results_variances.sigma_sq): print(k, v) # and the sigmas for the parameter estimation step are now known and fixed sigmas = results_variances.sigma_sq #========================================================================= # USER INPUT SECTION - PARAMETER ESTIMATION
# The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets. #A_set = [l for i,l in enumerate(opt_model.meas_lambdas) if (i % 4 == 0)] # For this small problem we do not need this, however it should be set for larger problems # This is the standard Chen et al. method used within KIPET and added here for comparison with # alternative method. Once can see that the results are quite different and it is not obvious which # method provides the correct results. On close inspection, the alternative method is closer, # however this may be due to the fact that we were able to get the device variance fairly accurately # This will provide a list of sigma values based on the different delta values evaluated. results_variances = v_estimator.run_opt( 'ipopt', method='Chen', tee=False, solver_opts=options, #num_points = num_points, tolerance=1e-10) #device_range = search_range) # Variances can then be displayed print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.P): print(k, v) sigmas = results_variances.sigma_sq for k, v in six.iteritems(sigmas): print(k, v) #=========================================================================
v_estimator.initialize_from_trajectory('S', results.S) options = {} A_set = [l for i, l in enumerate(model.meas_lambdas) if (i % 4 == 0)] # #: this will take a sweet-long time to solve. results_variances = v_estimator.run_opt('ipopt', tee=True, solver_options=options, tolerance=1e-5, max_iter=15, subset_lambdas=A_set, inputs_sub=inputs_sub, trajectories=trajs, jump=True, jump_times=jump_times, jump_states=jump_states, fixedy=True, fixedtraj=True, yfix=yfix, yfixtraj=yfixtraj, feed_times=feed_times ) print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.sigma_sq): print(k, v) sigmas = results_variances.sigma_sq
#subset_lambdas = A, solver_opts=options) # Now that we have the worst case device variance we wish to solve the maximum likelihood # problem with different values of delta based on this information and device information # for best and worst-case performance. We will solve for different values of model variance, # known as sigma, at each value. We can provide a range, based on the value from above and # device specifications and we can set a number of points to search within that range. best_possible_accuracy = 1e-8 search_range = (best_possible_accuracy, worst_case_device_var) num_points = 10 # This will provide a list of sigma values based on the different delta values evaluated. results_variances = v_estimator.run_opt( 'ipopt', method='direct_sigmas', tee=False, solver_opts=options, num_points=num_points, #subset_lambdas=A_set, device_range=search_range) # It is suggested that from this pool of solutions for different delta and sigma values, that the # best choice for sigma and delta will then be which provided the parameter values closest to # the initial guesses. # it is also possible to solve directly for sigmas based on a specific value for delta using delta = 1e-7 results_vest = v_estimator.solve_sigma_given_delta( 'ipopt', #subset_lambdas= A, solver_opts=options, tee=False,
options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 options['linear_solver'] = 'ma57' # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets. A_set = [l for i, l in enumerate(opt_model.meas_lambdas) if (i % 4 == 0)] # Finally we run the variance estimator using the arguments shown in Section 4.3.3 results_variances = v_estimator.run_opt('ipopt', tee=True, solver_opts=options, lsq_ipopt=True, tolerance=1e-7, max_iter=25, report_time=True, subset_lambdas=A_set) # Variances can then be displayed print("\nThe estimated variances are:\n") for k, v in six.iteritems(results_variances.sigma_sq): print(k, v) # and the sigmas for the parameter estimation step are now known and fixed sigmas = results_variances.sigma_sq #========================================================================= # USER INPUT SECTION - PARAMETER ESTIMATION #=========================================================================