def run_samples(ama_options, args, model_module): # TBD: This has evolved so there may be overlap between ama_options and args # Read xhats from xhatpath xhat_one = ciutils.read_xhat(args.xhatpath)["ROOT"] num_samples = args.num_samples zhats, seed = evaluate_sample_trees(xhat_one, num_samples, ama_options, InitSeed=0, model_module=model_module) confidence_level = args.confidence_level zhatbar = np.mean(zhats) s_zhat = np.std(np.array(zhats)) t_zhat = scipy.stats.t.ppf(confidence_level, len(zhats) - 1) eps_z = t_zhat * s_zhat / np.sqrt(len(zhats)) print('zhatbar: ', zhatbar) print('estimate: ', [zhatbar - eps_z, zhatbar + eps_z]) print('confidence_level', confidence_level) return zhatbar, eps_z
def test_MMW_constructor(self): options = self._get_base_options() xhat = ciutils.read_xhat(self.xhat_path) MMW = MMWci.MMWConfidenceIntervals(self.refmodelname, options['kwoptions'], xhat, options['num_batches'], batch_size = options["batch_size"], start = options['kwoptions']['num_scens'])
def test_MMW_running(self): options = _get_base_options() xhat = ciutils.read_xhat(self.xhat_path) MMW = MMWci.MMWConfidenceIntervals(refmodelname, options['opt'], xhat, options['num_batches']) r = MMW.run() s = round_pos_sig(r['std'], 2) bound = round_pos_sig(r['gap_inner_bound'], 2) self.assertEqual((s, bound), (43.0, 280.0))
def test_MMW_running(self): options = self._get_base_options() xhat = ciutils.read_xhat(self.xhat_path) MMW = MMWci.MMWConfidenceIntervals(self.refmodelname, options['opt'], xhat, options['num_batches'], batch_size=options["batch_size"], start=options['opt']['num_scens']) r = MMW.run() s = round_pos_sig(r['std'], 2) bound = round_pos_sig(r['gap_inner_bound'], 2) self.assertEqual((s, bound), (1.5, 96.0))
def test_xhat_eval_evaluate(self): options = self._get_xhatEval_options() MMW_options = self._get_base_options() scenario_creator_kwargs = MMW_options['kwargs'] scenario_creator_kwargs['num_scens'] = MMW_options['batch_size'] ev = Xhat_Eval(options, farmer.scenario_names_creator(100), farmer.scenario_creator, scenario_denouement=None, scenario_creator_kwargs=scenario_creator_kwargs) xhat = ciutils.read_xhat(self.xhat_path) obj = round_pos_sig(ev.evaluate(xhat), 2) self.assertEqual(obj, -1300000.0)
def test_xhat_eval_evaluate_one(self): options = self._get_xhatEval_options() MMW_options = self._get_base_options() xhat = ciutils.read_xhat(self.xhat_path) scenario_creator_kwargs = MMW_options['kwargs'] scenario_creator_kwargs['num_scens'] = MMW_options['batch_size'] scenario_names = farmer.scenario_names_creator(100) ev = Xhat_Eval(options, scenario_names, farmer.scenario_creator, scenario_denouement=None, scenario_creator_kwargs=scenario_creator_kwargs) k = scenario_names[0] obj = ev.evaluate_one(xhat, k, ev.local_scenarios[k]) obj = round_pos_sig(obj, 2) self.assertEqual(obj, -48000.0)
args = parser.parse_args() #parses solver options string solver_options = option_string_to_dict(args.solver_options) if args.num_scens == None: print('\n') raise Exception("Please include number of scenes used to compute the candidate solutions xhat.") print('\n') # convert instance path to module name: modelpath = re.sub('/','.', args.instance) modelpath = re.sub(r'\.py','', modelpath) # Read xhats from xhatpath xhat = ciutils.read_xhat(args.xhatpath) if args.batch_size == None: args.batch_size = args.num_scens refmodel = modelpath #Change this path to use a different model options = {"EF-2stage": True,# 2stage vs. mstage "start": False, "EF_solver_name": args.solver_name, "EF_solver_options": solver_options, "num_scens": args.num_scens} #Are the scenario shifted by a start arg ? #should we accept these as arguments? num_batches = args.num_batches
def test_xhat_read_write(self): path = tempfile.mkstemp(prefix="xhat", suffix=".npy")[1] ciutils.write_xhat(self.xhat, path=path) x = ciutils.read_xhat(path, delete_file=True) self.assertEqual(list(x['ROOT']), list(self.xhat['ROOT']))
ama_object.run() if global_rank == 0: print("inner bound=", ama_object.best_inner_bound) # This the xhat of the left term of LHS of MMW (9) print("outer bound=", ama_object.best_outer_bound) ########### get the nonants (the xhat) nonant_cache = sputils.nonant_cache_from_ef(ama_object.ef) ciutils.write_xhat(nonant_cache, path="xhat.npy") #Set parameters for run() options = ama_object.options options['solver_options'] = options['EF_solver_options'] xhat = ciutils.read_xhat("xhat.npy") num_batches = ama_object.options['num_batches'] batch_size = ama_object.options['batch_size'] mmw = MMWConfidenceIntervals(refmodel, options, xhat, num_batches, batch_size=batch_size, verbose=False) r = mmw.run() global_toc(r) if global_rank == 0: os.remove("xhat.npy")
def test_MMW_constructor(self): options = _get_base_options() xhat = ciutils.read_xhat(self.xhat_path) MMW = MMWci.MMWConfidenceIntervals(refmodelname, options['opt'], xhat, options['num_batches'])