示例#1
0
    def test_CopulaGaussian(self):
        df_corr = pd.DataFrame(dict(var1=["x"], var2=["y"], corr=[0.5]))
        Sigma_h = np.linalg.cholesky(np.array([[1.0, 0.5], [0.5, 1.0]]))
        copula = gr.CopulaGaussian(["x", "y"], df_corr=df_corr)
        df_res = copula.sample(seed=101)

        self.assertTrue(np.isclose(copula.Sigma_h, Sigma_h).all)
        self.assertTrue(set(df_res.columns) == set(["x", "y"]))

        ## Test raises
        df_corr_invalid = pd.DataFrame(
            dict(var1=["x", "x"], var2=["y", "z"], corr=[0, 0])
        )

        with self.assertRaises(ValueError):
            gr.CopulaGaussian(["x", "y"], df_corr=df_corr_invalid)

        ## 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))
示例#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]))),
        )
示例#3
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(
             pd.DataFrame(dict(var1=["x"], var2=["y"], corr=[0.5]))
         ),
     )