def test_samples_z2(): dist1 = Uniform(loc=0.0, scale=5.0) dist2 = Uniform(loc=0.0, scale=3.0) ntf_obj = Nataf(distributions=[dist1, dist2]) samples_z = np.array([[0.3, 1.2], [0.2, 2.4]]).T ntf_obj.run(samples_z=samples_z, jacobian=True) g = [] for i in range(2): if i == 0: g.append((ntf_obj.samples_x[i] == np.array( [3.089557110944763, 1.737779128317309])).all()) elif i == 1: g.append((ntf_obj.samples_x[i] == np.array( [4.424651648891459, 2.9754073922262116])).all()) assert np.all(g)
def test_non_identity_correlation_uniform_x(): dist1 = Uniform(loc=0.0, scale=1.0) dist2 = Uniform(loc=0.0, scale=1.0) rz = np.array([[1.0, 0.8], [0.8, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_z=rz) assert (ntf_obj.corr_x == [[1., 0.7859392826067285], [0.7859392826067285, 1.]]).all()
def test_non_identity_correlation_uniform_z(): dist1 = Uniform(loc=0.0, scale=1.0) dist2 = Uniform(loc=0.0, scale=1.0) rx = np.array([[1.0, 0.8], [0.8, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) np.testing.assert_allclose( ntf_obj.corr_z, [[1., 0.8134732861515996], [0.8134732861515996, 1.]], rtol=1e-09)
def test_samples_x1(): dist1 = Uniform(loc=0.0, scale=5.0) dist2 = Uniform(loc=0.0, scale=3.0) ntf_obj = Nataf(distributions=[dist1, dist2]) samples_x = np.array([[0.3, 1.2, 3.5], [0.2, 2.4, 0.9]]).T ntf_obj.run(samples_x=samples_x, jacobian=True) g = [] for i in range(3): if i == 0: g.append((ntf_obj.samples_z[i] == np.array( [-1.5547735945968535, -1.501085946044025])).all()) elif i == 1: g.append((ntf_obj.samples_z[i] == np.array( [-0.7063025628400874, 0.841621233572914])).all()) else: g.append((ntf_obj.samples_z[i] == np.array( [0.5244005127080407, -0.5244005127080409])).all()) assert np.all(g)
def test_derivatives_1_no_samples(): dist1 = Normal(loc=200, scale=20) dist2 = Normal(loc=150, scale=10) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) for file_name in glob.glob("Model_Runs_*"): shutil.rmtree(file_name) with pytest.raises(Exception): assert TaylorSeries._derivatives(nataf_object=ntf_obj, runmodel_object=model_i)
def test_samples_z_jzx2(): dist1 = Uniform(loc=0.0, scale=5.0) dist2 = Uniform(loc=0.0, scale=3.0) ntf_obj = Nataf(distributions=[dist1, dist2]) samples_z = np.array([[0.3, 1.2], [0.2, 2.4]]).T ntf_obj.run(samples_z=samples_z, jacobian=True) g = [] for i in range(2): if i == 0: g.append( (ntf_obj.jzx[i] == np.array([[0.524400601939789, 0.0], [0.0, 0.8524218415758338]])).all()) else: g.append( (ntf_obj.jzx[i] == np.array([[1.0299400748281828, 0.0], [0.0, 14.884586948005541]])).all()) assert np.all(g)
def test_derivatives_6_second(): model = PythonModel(model_script='pfn2.py', model_object_name='model_j', delete_files=True) h_func = RunModel(model=model) dist1 = Normal(loc=500, scale=100) dist2 = Normal(loc=1000, scale=100) point_u = np.array([1.73673009, 0.16383283]) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) hessian = TaylorSeries._derivatives(point_u=point_u, runmodel_object=h_func, nataf_object=ntf_obj, order='second') np.testing.assert_allclose(hessian, [[-0.00720754, 0.00477726], [0.00477726, -0.00316643]], rtol=1e-04)
def test_samples_x_jxz2(): dist1 = Uniform(loc=0.0, scale=5.0) dist2 = Uniform(loc=0.0, scale=3.0) ntf_obj = Nataf(distributions=[dist1, dist2]) samples_x = np.array([[0.3, 1.2, 3.5], [0.2, 2.4, 0.9]]).T ntf_obj.run(samples_x=samples_x, jacobian=True) g = [] for i in range(3): if i == 0: g.append((ntf_obj.jxz[i] == np.array([[1.6789373877365803, 0.0], [0.0, 2.577850090371836]])).all()) elif i == 1: g.append( (ntf_obj.jxz[i] == np.array([[0.6433491348614259, 0.0], [0.0, 1.1906381155257868]])).all()) else: g.append((ntf_obj.jxz[i] == np.array([[0.5752207318528584, 0.0], [0.0, 0.958701219754764]])).all()) assert np.all(g)
def test_derivatives_5_run_model(setup): dist1 = Normal(loc=200, scale=20) dist2 = Normal(loc=150, scale=10) point_u = np.array([-2, 1]) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) gradient, qoi, array_of_samples = TaylorSeries._derivatives(point_u=point_u, runmodel_object=setup, nataf_object=ntf_obj) for file_name in glob.glob("Model_Runs_*"): shutil.rmtree(file_name) np.testing.assert_allclose(array_of_samples[0], [160, 160], rtol=1e-09) np.testing.assert_allclose(gradient, [20, -10], rtol=1e-09)
def test_distortion_z2x_finite_moments(): dist1 = Lognormal(s=0.0, loc=0.0, scale=1.0) dist2 = Uniform(loc=0.0, scale=1.0) rz = np.array([[1.0, 0.8], [0.8, 1.0]]) with pytest.raises(Exception): assert Nataf(distributions=[dist1, dist2], corr_z=rz)
# The two distributions are correlated according to the correlation matrix :code:`Rx`. # %% dist1 = Gamma(4.0, loc=0.0, scale=1.0) dist2 = Lognormal(s=2., loc=0., scale=np.exp(1)) Rx = np.array([[1.0, 0.9], [0.9, 1.0]]) # %% md # # Next, we'll construct a :code:`Nataf` object :code:`nataf_obj`. Here, we provide the distribution of the random # variables :code:`distributions` and the correlation matrix :code:`corr_x` in the parameter space. # %% nataf_obj = Nataf(distributions=[dist1, dist2], corr_x=Rx) # %% md # # We can use the :code:`rvs` method of the :code:`nataf_obj` object to draw random samples from the two distributions. # %% samples_x = nataf_obj.rvs(1000) # %% md # # We can visualize the samples by plotting them on axes of each distribution's range. # %%
def test_samples_x(): dist1 = Normal(loc=0.0, scale=1.0) ntf_obj = Nataf(distributions=[dist1]) assert ntf_obj.samples_x is None
def test_attribute_h(): dist1 = Normal(loc=0.0, scale=1.0) dist2 = Normal(loc=0.0, scale=1.0) rz = np.array([[1.0, 0.8], [0.8, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_z=rz) np.testing.assert_allclose(ntf_obj.H, [[1., 0.], [0.8, 0.6]], rtol=1e-09)
def test_correct_distribution_dimensions(): dist1 = Normal(loc=0.0, scale=1.0) dist2 = Normal(loc=0.0, scale=1.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) assert ntf_obj.n_dimensions == 2
def test_wrong_distribution_in_list(): dist1 = Normal(loc=0.0, scale=1.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) with pytest.raises(Exception): assert Nataf(distributions=[dist1, 'Beta'], corr_x=rx)
def test_samples_z_jzx1(): dist1 = Normal(loc=0.0, scale=5.0) ntf_obj = Nataf(distributions=[dist1]) samples_z = np.array([0.3, 1.2, 3.5]) ntf_obj.run(samples_z=samples_z, jacobian=False) assert ntf_obj.jzx is None
def test_non_identity_correlation_normal(): dist1 = Normal(loc=0.0, scale=1.0) dist2 = Normal(loc=0.0, scale=1.0) rx = np.array([[1.0, 0.8], [0.8, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) assert np.all(np.equal(ntf_obj.corr_z, rx))
def test_identity_correlation_uniform_z(): dist1 = Uniform(loc=0.0, scale=1.0) dist2 = Uniform(loc=0.0, scale=1.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1, dist2], corr_x=rx) assert np.all(np.equal(ntf_obj.corr_z, rx))
def test_identity_correlation_z_normal(): dist1 = Normal(loc=0.0, scale=1.0) dist2 = Normal(loc=0.0, scale=1.0) ntf_obj = Nataf(distributions=[dist1, dist2]) assert np.all(np.equal(ntf_obj.corr_z, np.eye(2)))
def test_itam_threshold2_1(): dist1 = Uniform(loc=0.0, scale=5.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1], corr_x=rx, itam_threshold2=0.3) assert ntf_obj.itam_threshold2 == 0.3
def test_itam_max_iteration2(): dist1 = Uniform(loc=0.0, scale=5.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1], corr_x=rx, itam_max_iter=10) assert ntf_obj.itam_max_iter == 10
def test_itam_beta1(): dist1 = Uniform(loc=0.0, scale=5.0) rx = np.array([[1.0, 0.0], [0.0, 1.0]]) ntf_obj = Nataf(distributions=[dist1], corr_x=rx, itam_beta=2.0) assert ntf_obj.itam_beta == 2.0
def test_wrong_distribution(): rx = np.array([[1.0, 0.0], [0.0, 1.0]]) with pytest.raises(Exception): assert Nataf(distributions='Normal', corr_x=rx)
def distortion_z2x_dist_object(): dist1 = Lognormal(s=0.0, loc=0.0, scale=1.0) rz = np.array([[1.0, 0.8], [0.8, 1.0]]) with pytest.raises(Exception): assert Nataf.distortion_z2x(distributions=[dist1, 'Beta'], corr_z=rz)
def test_samples_z_shape(): dist1 = Normal(loc=0.0, scale=5.0) ntf_obj = Nataf(distributions=[dist1]) samples_z = np.array([0.3, 1.2, 3.5]) ntf_obj.run(samples_z) assert ntf_obj.samples_z.shape == (3, 1)