Example #1
0
    def setUp(self):
        # 2D identity model with permuted df inputs
        domain_2d = gr.Domain(bounds={"x": [-1.0, +1], "y": [0.0, 1.0]})
        marginals = {}
        marginals["x"] = gr.MarginalNamed(
            d_name="uniform", d_param={"loc": -1, "scale": 2}
        )
        marginals["y"] = gr.MarginalNamed(
            sign=-1, d_name="uniform", d_param={"loc": 0, "scale": 1}
        )

        self.model_2d = gr.Model(
            functions=[
                gr.Function(lambda x: [x[0], x[1]], ["x", "y"], ["f", "g"], "test", 0)
            ],
            domain=domain_2d,
            density=gr.Density(
                marginals=marginals, copula=gr.CopulaIndependence(var_rand=["x"])
            ),
        )

        ## Correct results
        self.df_2d_nominal = pd.DataFrame(
            data={"x": [0.0], "y": [0.5], "f": [0.0], "g": [0.5]}
        )
        self.df_2d_grad = pd.DataFrame(
            data={"Df_Dx": [1.0], "Dg_Dx": [0.0], "Df_Dy": [0.0], "Dg_Dy": [1.0]}
        )
        self.df_2d_qe = pd.DataFrame(
            data={"x": [0.0], "y": [0.1], "f": [0.0], "g": [0.1]}
        )
Example #2
0
    def setUp(self):
        self.density = gr.Density(
            marginals=dict(
                x=gr.MarginalNamed(d_name="uniform",
                                   d_param={
                                       "loc": -1,
                                       "scale": 2
                                   }),
                y=gr.MarginalNamed(d_name="uniform",
                                   d_param={
                                       "loc": -1,
                                       "scale": 2
                                   }),
            ),
            copula=gr.CopulaGaussian(["x", "y"],
                                     pd.DataFrame(
                                         dict(var1=["x"],
                                              var2=["y"],
                                              corr=[0.5]))),
        )

        self.density_ind = gr.Density(
            marginals=dict(
                x=gr.MarginalNamed(d_name="uniform",
                                   d_param={
                                       "loc": -1,
                                       "scale": 2
                                   }),
                y=gr.MarginalNamed(d_name="norm",
                                   d_param={
                                       "loc": 0,
                                       "scale": 1
                                   }),
            ),
            copula=gr.CopulaIndependence(["x", "y"]),
        )
        self.density_gauss = gr.Density(
            marginals=dict(
                x=gr.MarginalNamed(d_name="uniform",
                                   d_param={
                                       "loc": -1,
                                       "scale": 2
                                   }),
                y=gr.MarginalNamed(d_name="norm",
                                   d_param={
                                       "loc": 0,
                                       "scale": 1
                                   }),
            ),
            copula=gr.CopulaGaussian(["x", "y"],
                                     pd.DataFrame(
                                         dict(var1=["x"],
                                              var2=["y"],
                                              corr=[0.5]))),
        )
Example #3
0
    def test_CopulaIndependence(self):
        copula = gr.CopulaIndependence(var_rand=["x", "y"])
        df_res = copula.sample(seed=101)

        self.assertTrue(set(df_res.columns) == set(["x", "y"]))

        ## Transforms invariant
        z = np.array([0, 0])
        u = copula.z2u(z)
        zp = copula.u2z(u)

        self.assertTrue(np.all(z == zp))

        ## Jacobian accurate
        dudz_fd = np.zeros((2, 2))
        dudz_fd[0, :] = (copula.z2u(z + np.array([h, 0])) - copula.z2u(z)) / h
        dudz_fd[1, :] = (copula.z2u(z + np.array([0, h])) - copula.z2u(z)) / h
        dudz_p = copula.dudz(z)

        self.assertTrue(np.allclose(dudz_fd, dudz_p))