Esempio n. 1
0
 def test_sample_highest_prob(self):
     """
     Test :meth:`bet.postProcess.postTools.sample_highest_prob`.
     """
     (num_samples,P_samples, samples, _ , data) = postTools.sample_highest_prob(1.0,
                                                                               self.P_samples, 
                                                                               self.samples,
                                                                               lam_vol=None, data=self.data, sort=True)
     nptest.assert_almost_equal(np.sum(P_samples),1.0)
     nptest.assert_equal(num_samples,1000)
     
     (num_samples,P_samples, samples, _ , data) = postTools.sample_highest_prob(0.8,
                                                                               self.P_samples, 
                                                                               self.samples,
                                                                               lam_vol=None, data=self.data, sort=True)
     nptest.assert_allclose(np.sum(P_samples),0.8,0.001)
Esempio n. 2
0
    def test_sample_highest_prob(self):
        """
        Test :meth:`bet.postProcess.postTools.sample_highest_prob`.
        """
        (num_samples, sample_set_out, _) = postTools.sample_highest_prob(1.0,
                                                                         self.data,
                                                                         sort=True)

        nptest.assert_almost_equal(
            np.sum(sample_set_out.get_probabilities()), 1.0)
        nptest.assert_equal(num_samples, 1000)

        (num_samples, sample_set_out, _) = postTools.sample_highest_prob(0.8,
                                                                         self.data,
                                                                         sort=True)

        nptest.assert_allclose(
            np.sum(sample_set_out.get_probabilities()), 0.8, 0.001)
Q_ref = Q[QoI_indices, :].dot(0.5 * np.ones(input_dim))

# bin_ratio defines the uncertainty in our data
bin_ratio = 0.25

# Create discretization object
my_discretization = sample.discretization(input_sample_set=input_samples,
                                        output_sample_set=output_samples)


# Find the simple function approximation
simpleFunP.regular_partition_uniform_distribution_rectangle_scaled(
    data_set=my_discretization, Q_ref=Q_ref, rect_scale=bin_ratio,
    cells_per_dimension=1)

# Calculate probablities making the Monte Carlo assumption
calculateP.prob(my_discretization)

percentile = 1.0
# Sort samples by highest probability density and find how many samples lie in
# the support of the inverse solution.  With the Monte Carlo assumption, this
# also tells us the approximate volume of this support.
(num_samples, _, indices_in_inverse) =\
    postTools.sample_highest_prob(top_percentile=percentile,
    sample_set=input_samples, sort=True)

# Print the number of samples that make up the highest percentile percent
# samples and ratio of the volume of the parameter domain they take up
if comm.rank == 0:
    print (num_samples, np.sum(input_samples.get_volumes()[indices_in_inverse]))
Esempio n. 4
0
# calculate 1d marginal probs
(bins, marginals1D) = plotP.calculate_1D_marginal_probs(P_samples=P, samples=samples, lam_domain=lam_domain, nbins=20)

# smooth 1d marginal probs (optional)
marginals1D = plotP.smooth_marginals_1D(marginals1D, bins, sigma=1.0)

# plot 1d marginal probs
plotP.plot_1D_marginal_probs(
    marginals1D, bins, lam_domain, filename="contaminant_map", interactive=False, lam_ref=ref_lam, lambda_label=labels
)

percentile = 1.0
# Sort samples by highest probability density and sample highest percentile percent samples
(num_samples, P_high, samples_high, lam_vol_high, data_high) = postTools.sample_highest_prob(
    top_percentile=percentile, P_samples=P, samples=samples, lam_vol=lam_vol, data=data, sort=True
)

# print the number of samples that make up the  highest percentile percent samples and
# ratio of the volume of the parameter domain they take up
print(num_samples, np.sum(lam_vol_high))

# Propogate the probability measure through a different QoI map
(_, P_pred, _, _, data_pred) = postTools.sample_highest_prob(
    top_percentile=percentile, P_samples=P, samples=samples, lam_vol=lam_vol, data=dataf[:, 7], sort=True
)

# Plot 1D pdf of predicted QoI
# calculate 1d marginal probs
(bins_pred, marginals1D_pred) = plotP.calculate_1D_marginal_probs(
    P_samples=P_pred, samples=data_pred, lam_domain=np.array([[np.min(data_pred), np.max(data_pred)]]), nbins=20
Esempio n. 5
0
#QoI_indices = [0, 3, 5, 8, 9]
#QoI_indices = [3, 4, 5, 8, 9]
#QoI_indices = [2, 3, 5, 6, 9]

# Restrict the data to have just QoI_indices
data = data[:, QoI_indices]
Q_ref = Q[QoI_indices, :].dot(0.5 * np.ones(Lambda_dim))
# bin_ratio defines the uncertainty in our data
bin_ratio = 0.25

# Find the simple function approximation
(d_distr_prob, d_distr_samples, d_Tree) = simpleFunP.uniform_hyperrectangle(\
    data=data, Q_ref=Q_ref, bin_ratio=bin_ratio, center_pts_per_edge = 1)

# Calculate probablities making the Monte Carlo assumption
(P,  lam_vol, io_ptr) = calculateP.prob(samples=samples, data=data,
    rho_D_M=d_distr_prob, d_distr_samples=d_distr_samples)

percentile = 1.0
# Sort samples by highest probability density and find how many samples lie in
# the support of the inverse solution.  With the Monte Carlo assumption, this
# also tells us the approximate volume of this support.
(num_samples, P_high, samples_high, lam_vol_high, data_high) =\
    postTools.sample_highest_prob(top_percentile=percentile, P_samples=P,
    samples=samples, lam_vol=lam_vol,data = data,sort=True)

# Print the number of samples that make up the highest percentile percent
# samples and ratio of the volume of the parameter domain they take up
if comm.rank == 0:
    print (num_samples, np.sum(lam_vol_high))
Esempio n. 6
0
# calculate 1d marginal probs
(bins, marginals1D) = plotP.calculate_1D_marginal_probs(my_discretization, nbins = 20)

# smooth 1d marginal probs (optional)
marginals1D = plotP.smooth_marginals_1D(marginals1D, bins, sigma=1.0)

# plot 1d marginal probs
plotP.plot_1D_marginal_probs(marginals1D, bins, my_discretization,
                             filename = "contaminant_map",
                             interactive=False,
                             lam_ref=param_ref,
                             lambda_label=labels)

percentile = 1.0
# Sort samples by highest probability density and sample highest percentile percent samples
(num_samples, my_discretization_highP, indices)= postTools.sample_highest_prob(
    percentile, my_discretization, sort=True)

# print the number of samples that make up the  highest percentile percent samples and
# ratio of the volume of the parameter domain they take up
print (num_samples, np.sum(my_discretization_highP._input_sample_set.get_volumes()))

# Choose unused QoI as prediction QoI and propagate measure onto predicted QoI data space
QoI_indices_predict = np.array([7])
output_samples_predict = samp.sample_set(QoI_indices_predict.size)
output_samples_predict.set_values(np.loadtxt("files/data.txt.gz")[:,QoI_indices_predict])
output_samples_predict.set_probabilities(input_samples.get_probabilities())

# Determine range of predictions and store as domain for plotting purposes
output_samples_predict.set_domain(output_samples_predict.get_bounding_box())

# Plot 1D pdf of predicted QoI
Esempio n. 7
0
Q_ref = Q[QoI_indices, :].dot(0.5 * np.ones(input_dim))

# Define the level of uncertainty in the measured reference datum
rect_scale = 0.25

# Make the MC assumption and compute the volumes of each voronoi cell
input_samples.estimate_volume_mc()

# Create discretization object
my_discretization = sample.discretization(input_sample_set=input_samples,
                                        output_sample_set=output_samples)


# Find the simple function approximation
simpleFunP.regular_partition_uniform_distribution_rectangle_scaled(
    data_set=my_discretization, Q_ref=Q_ref, rect_scale=rect_scale,
    cells_per_dimension=1)

# Compute the solution to the stochastic inverse problem
calculateP.prob(my_discretization)

(_, _, indices_in_inverse) = \
    postTools.sample_highest_prob(top_percentile=1.0,
                                  sample_set=input_samples, sort=True)

# Print the approximate percentage of the measure of the parameter space defined
# by the support of the inverse density
if comm.rank == 0:
    print 'The approximate percentage of the measure of the parameter space defined'
    print 'by the support of the inverse density associated with the choice of QoI map is'
    print np.sum(input_samples.get_volumes()[indices_in_inverse])