def test_methods(self): calcP.prob_from_sample_set_with_emulated_volumes(self.set_old, self.set_new, self.set_em) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) calcP.prob_from_sample_set(self.set_old, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) disc = samp.discretization(input_sample_set=self.set_old, output_sample_set=self.set_old) calcP.prob_from_discretization_input(disc, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) num_em = self.set_em.check_num() probs = np.zeros((num_em,)) probs[0:-1] = 1.0/float(num_em-1) self.set_em.set_probabilities(probs) self.set_em.global_to_local() disc = samp.discretization(input_sample_set=self.set_old, output_sample_set=self.set_old, emulated_input_sample_set=self.set_em) calcP.prob_from_discretization_input(disc, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75])
def test_methods(self): calcP.prob_from_sample_set_with_emulated_volumes( self.set_old, self.set_new, self.set_em) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) calcP.prob_from_sample_set(self.set_old, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) disc = samp.discretization(input_sample_set=self.set_old, output_sample_set=self.set_old) calcP.prob_from_discretization_input(disc, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75]) num_em = self.set_em.check_num() probs = np.zeros((num_em, )) probs[0:-1] = 1.0 / float(num_em - 1) self.set_em.set_probabilities(probs) self.set_em.global_to_local() disc = samp.discretization(input_sample_set=self.set_old, output_sample_set=self.set_old, emulated_input_sample_set=self.set_em) calcP.prob_from_discretization_input(disc, self.set_new) nptest.assert_almost_equal(self.set_new._probabilities, [0.25, 0.75])
def calculate_prob_for_sample_set_region(self, s_set, regions, update_input=True): """ Solves stochastic inverse problem based on surrogate points and the MC assumption. Calculates the probability of a regions of input space and error estimates for those probabilities. :param: s_set: sample set for which to calculate error :type s_set: :class:`bet.sample.sample_set_base` :param region: list of regions of s_set for which to calculate error :type region: list :param update_input: whether or not to update probabilities and errror identifiers for input discretization :type update_input: bool :rtype: tuple :returns: (probabilities, ``error_estimates``), the probability and error estimates for the region """ if not hasattr(self, 'surrogate_discretization'): msg = "surrogate discretization has not been created" raise calculateError.wrong_argument_type(msg) if not isinstance(s_set, sample.sample_set_base): msg = "s_set must be of type bet.sample.sample_set_base" raise calculateError.wrong_argument_type(msg) # Calculate probability of region if self.surrogate_discretization._input_sample_set._volumes_local\ is None: self.surrogate_discretization._input_sample_set.\ estimate_volume_mc(globalize=False) calculateP.prob(self.surrogate_discretization, globalize=False) prob_new_values = calculateP.prob_from_sample_set(\ self.surrogate_discretization._input_sample_set, s_set) # Calculate for each region probabilities = [] error_estimates = [] for region in regions: marker = np.equal(s_set._region, region) probability = np.sum(prob_new_values[marker]) # Calculate error estimate for region model_error = calculateError.model_error(\ self.surrogate_discretization) error_estimate = model_error.calculate_for_sample_set_region_mc(\ s_set, region) probabilities.append(probability) error_estimates.append(error_estimate) # Update input only if 1 region is given if update_input: num = self.input_disc._input_sample_set.check_num() prob = np.zeros((num, )) error_id = np.zeros((num, )) for i in range(num): Itemp = np.equal(self.dummy_disc._emulated_ii_ptr_local, i) prob_sum = np.sum(self.surrogate_discretization.\ _input_sample_set._probabilities_local[Itemp]) prob[i] = comm.allreduce(prob_sum, op=MPI.SUM) error_id_sum = np.sum(self.surrogate_discretization.\ _input_sample_set._error_id_local[Itemp]) error_id[i] = comm.allreduce(error_id_sum, op=MPI.SUM) self.input_disc._input_sample_set.set_probabilities(prob) self.input_disc._input_sample_set.set_error_id(error_id) return (probabilities, error_estimates)
def calculate_prob_for_sample_set_region(self, s_set, regions, update_input=True): """ Solves stochastic inverse problem based on surrogate points and the MC assumption. Calculates the probability of a regions of input space and error estimates for those probabilities. :param: s_set: sample set for which to calculate error :type s_set: :class:`bet.sample.sample_set_base` :param region: list of regions of s_set for which to calculate error :type region: list :param update_input: whether or not to update probabilities and errror identifiers for input discretization :type update_input: bool :rtype: tuple :returns: (probabilities, ``error_estimates``), the probability and error estimates for the region """ if not hasattr(self, 'surrogate_discretization'): msg = "surrogate discretization has not been created" raise calculateError.wrong_argument_type(msg) if not isinstance(s_set, sample.sample_set_base): msg = "s_set must be of type bet.sample.sample_set_base" raise calculateError.wrong_argument_type(msg) # Calculate probability of region if self.surrogate_discretization._input_sample_set._volumes_local\ is None: self.surrogate_discretization._input_sample_set.\ estimate_volume_mc(globalize=False) calculateP.prob(self.surrogate_discretization, globalize=False) prob_new_values = calculateP.prob_from_sample_set(\ self.surrogate_discretization._input_sample_set, s_set) # Calculate for each region probabilities = [] error_estimates = [] for region in regions: marker = np.equal(s_set._region, region) probability = np.sum(prob_new_values[marker]) # Calculate error estimate for region model_error = calculateError.model_error(\ self.surrogate_discretization) error_estimate = model_error.calculate_for_sample_set_region_mc(\ s_set, region) probabilities.append(probability) error_estimates.append(error_estimate) # Update input only if 1 region is given if update_input: num = self.input_disc._input_sample_set.check_num() prob = np.zeros((num,)) error_id = np.zeros((num,)) for i in range(num): Itemp = np.equal(self.dummy_disc._emulated_ii_ptr_local, i) prob_sum = np.sum(self.surrogate_discretization.\ _input_sample_set._probabilities_local[Itemp]) prob[i] = comm.allreduce(prob_sum, op=MPI.SUM) error_id_sum = np.sum(self.surrogate_discretization.\ _input_sample_set._error_id_local[Itemp]) error_id[i] = comm.allreduce(error_id_sum, op=MPI.SUM) self.input_disc._input_sample_set.set_probabilities(prob) self.input_disc._input_sample_set.set_error_id(error_id) return (probabilities, error_estimates)
# Set in input space to calculate errors for s_set = samp.rectangle_sample_set(3) s_set.setup(maxes=[[0.75, 0.75, 0.75]], mins=[[0.25, 0.25, 0.25]]) s_set.set_region(np.array([0,1])) # Calculate true probabilty using linear surrogate on true data sur = surrogates.piecewise_polynomial_surrogate(my_disc_exact) sur_disc_lin = sur.generate_for_input_set(emulated_inputs, order=1) (Pt, _) = sur.calculate_prob_for_sample_set_region(s_set, regions=[0]) P_true = Pt[0] if comm.rank == 0: print "True probability: ", P_true # Approximate probabilty P_approx = calculateP.prob_from_sample_set(my_disc._input_sample_set, s_set)[0] if comm.rank == 0: print "Approximate probability: ", P_approx # Calculate sampling error samp_er = calculateError.sampling_error(my_disc) (up, low) = samp_er.calculate_for_sample_set_region(s_set, region=0, emulated_set=emulated_inputs) if comm.rank == 0: print "Sampling error upper bound: ", up print "Sampling error lower bound: ", low # Calculate modeling error mod_er = calculateError.model_error(my_disc) er_est = mod_er.calculate_for_sample_set_region(s_set,
# Set in input space to calculate errors for s_set = samp.rectangle_sample_set(3) s_set.setup(maxes=[[0.75, 0.75, 0.75]], mins=[[0.25, 0.25, 0.25]]) s_set.set_region(np.array([0, 1])) # Calculate true probabilty using linear surrogate on true data sur = surrogates.piecewise_polynomial_surrogate(my_disc_exact) sur_disc_lin = sur.generate_for_input_set(emulated_inputs, order=1) (Pt, _) = sur.calculate_prob_for_sample_set_region(s_set, regions=[0]) P_true = Pt[0] if comm.rank == 0: print "True probability: ", P_true # Approximate probabilty P_approx = calculateP.prob_from_sample_set(my_disc._input_sample_set, s_set)[0] if comm.rank == 0: print "Approximate probability: ", P_approx # Calculate sampling error samp_er = calculateError.sampling_error(my_disc) (up, low) = samp_er.calculate_for_sample_set_region(s_set, region=0, emulated_set=emulated_inputs) if comm.rank == 0: print "Sampling error upper bound: ", up print "Sampling error lower bound: ", low # Calculate modeling error mod_er = calculateError.model_error(my_disc)