def test_derivatives_scalarQoI(self): systemsize = 3 mu = mean_3dim # np.random.randn(systemsize) std_dev = std_dev_3dim # np.diag(np.random.rand(systemsize)) jdist = cp.MvNormal(mu, std_dev) # Create QoI Object QoI = examples.Paraboloid3D(systemsize) # Create the Stochastic Collocation object deriv_dict = { 'xi': { 'dQoI_func': QoI.eval_QoIGradient, 'output_dimensions': systemsize } } QoI_dict = { 'paraboloid': { 'quadrature_degree': 3, 'reduced_collocation': False, 'QoI_func': QoI.eval_QoI, 'output_dimensions': 1, 'include_derivs': True, 'deriv_dict': deriv_dict } } sc_obj = StochasticCollocation3(jdist, 'MvNormal', QoI_dict) sc_obj.evaluateQoIs(jdist) dmu_j = sc_obj.dmean(of=['paraboloid'], wrt=['xi']) # dvar_j = sc_obj.dvariance(of=['paraboloid'], wrt=['xi']) # Analytical dmu_j dmu_j_analytical = np.array([100 * mu[0], 50 * mu[1], 2 * mu[2]]) err = abs( (dmu_j['paraboloid']['xi'] - dmu_j_analytical) / dmu_j_analytical) self.assertTrue((err < 1.e-12).all())
def test_reduced_normalStochasticCollocation3D(self): # This is not a very good test because we are comparing the reduced collocation # against the analytical expected value. The only hting it tells us is that # the solution is within the ball park of actual value. We still need to # come up with a better test. systemsize = 3 mu = mean_3dim # np.random.randn(systemsize) std_dev = std_dev_3dim # abs(np.diag(np.random.randn(systemsize))) jdist = cp.MvNormal(mu, std_dev) # Create QoI Object QoI = examples.Paraboloid3D(systemsize) dominant_dir = np.array([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0]], dtype=np.float) # Create the Stochastic Collocation object deriv_dict = { 'xi': { 'dQoI_func': QoI.eval_QoIGradient, 'output_dimensions': systemsize } } QoI_dict = { 'paraboloid': { 'QoI_func': QoI.eval_QoI, 'output_dimensions': 1, 'deriv_dict': deriv_dict } } sc_obj = StochasticCollocation2(jdist, 3, 'MvNormal', QoI_dict, reduced_collocation=True, dominant_dir=dominant_dir) sc_obj.evaluateQoIs(jdist) mu_js = sc_obj.mean(of=['paraboloid']) var_js = sc_obj.variance(of=['paraboloid']) # Analytical mean mu_j_analytical = QoI.eval_QoI_analyticalmean(mu, cp.Cov(jdist)) err = abs((mu_js['paraboloid'][0] - mu_j_analytical) / mu_j_analytical) self.assertTrue(err < 1e-1) # Analytical variance var_j_analytical = QoI.eval_QoI_analyticalvariance(mu, cp.Cov(jdist)) err = abs( (var_js['paraboloid'][0, 0] - var_j_analytical) / var_j_analytical) self.assertTrue(err < 0.01)
def test_central_difference(self): systemsize = 3 QoI = examples.Paraboloid3D(systemsize) def func(x): return QoI.eval_QoI(x, np.zeros(systemsize)) x_location = np.random.randn(3) # Do the finite difference dfval = utils.central_fd(func, x_location, output_dimensions=1, fd_pert=1.e-6) # Get the analytical gradient dfval_analytical = QoI.eval_QoIGradient(x_location, np.zeros(3)) err = abs(dfval - dfval_analytical) self.assertTrue((err < 1.e-6).all())
def test_normalStochasticCollocation3D(self): systemsize = 3 mu = mean_3dim # np.random.randn(systemsize) std_dev = std_dev_3dim # np.diag(np.random.rand(systemsize)) jdist = cp.MvNormal(mu, std_dev) # Create QoI Object QoI = examples.Paraboloid3D(systemsize) # Create the Stochastic Collocation object deriv_dict = { 'xi': { 'dQoI_func': QoI.eval_QoIGradient, 'output_dimensions': systemsize } } QoI_dict = { 'paraboloid': { 'quadrature_degree': 3, 'reduced_collocation': False, 'QoI_func': QoI.eval_QoI, 'output_dimensions': 1, 'include_derivs': False, 'deriv_dict': deriv_dict } } sc_obj = StochasticCollocation3(jdist, 'MvNormal', QoI_dict) sc_obj.evaluateQoIs(jdist) mu_js = sc_obj.mean(of=['paraboloid']) var_js = sc_obj.variance(of=['paraboloid']) # Analytical mean mu_j_analytical = QoI.eval_QoI_analyticalmean(mu, cp.Cov(jdist)) err = abs((mu_js['paraboloid'][0] - mu_j_analytical) / mu_j_analytical) self.assertTrue(err < 1.e-15) # Analytical variance var_j_analytical = QoI.eval_QoI_analyticalvariance(mu, cp.Cov(jdist)) err = abs( (var_js['paraboloid'][0, 0] - var_j_analytical) / var_j_analytical) self.assertTrue(err < 1.e-15)
from smt.surrogate_models import QP # Surrogate Modeling from pystatreduce.new_stochastic_collocation import StochasticCollocation2 from pystatreduce.stochastic_collocation import StochasticCollocation from pystatreduce.quantity_of_interest import QuantityOfInterest from pystatreduce.dimension_reduction import DimensionReduction import pystatreduce.examples as examples import pystatreduce.utils as utils # Create the actual QoI systemsize = 3 mu = np.random.randn(3) std_dev = abs(np.diag(np.random.randn(3))) jdist = cp.MvNormal(mu, std_dev) QoI = examples.Paraboloid3D(systemsize) # Generate the samples for surrogate n_surrogate_samples = int(0.5 * (systemsize + 1) * (systemsize + 2)) surrogate_samples = jdist.sample(5000, rule='R') std_dev_vec = np.diagonal(std_dev) new_samples = utils.check_std_dev_violation(surrogate_samples, mu, std_dev_vec, scale=1.0) print('orig_shape = ', new_samples.shape) new_samples = new_samples[:,0:1000] print('new_shape = ', new_samples.shape) """ fval_arr = np.zeros(n_surrogate_samples) for i in range(n_surrogate_samples): fval_arr[i] = QoI.eval_QoI(surrogate_samples[:,i], np.zeros(systemsize))