Exemple #1
0
    def test_sample(self):
        ## Accurate
        n = 2
        df_res = gr.eval_sample(self.md, n=n, df_det="nom", seed=101)

        np.random.seed(101)
        df_truth = pd.DataFrame({"x0": np.random.random(n)})
        df_truth["y0"] = df_truth["x0"]

        self.assertTrue(gr.df_equal(df_res, df_truth))

        ## Rounding
        df_round = gr.eval_sample(self.md, n=n + 0.1, df_det="nom", seed=101)

        self.assertTrue(gr.df_equal(df_round, df_truth))

        ## Pass-through
        df_pass = gr.eval_sample(self.md,
                                 n=n,
                                 skip=True,
                                 df_det="nom",
                                 seed=101)

        self.assertTrue(gr.df_equal(df_pass[["x0"]], df_truth[["x0"]]))

        ## Optional observation index
        df_idx = gr.eval_sample(
            self.md_mixed,
            n=n,
            df_det=gr.df_make(x0=[-1, 0, 1]),
            seed=101,
            index="idx",
        )

        self.assertTrue(len(set(df_idx.idx)) == n)
Exemple #2
0
    def test_monte_carlo(self):
        ## Accurate
        n = 2
        df_res = gr.eval_monte_carlo(self.md, n=n, df_det="nom", seed=101)

        np.random.seed(101)
        df_truth = pd.DataFrame({"x0": np.random.random(n)})
        df_truth["y0"] = df_truth["x0"]

        self.assertTrue(gr.df_equal(df_res, df_truth))

        ## Rounding
        df_round = gr.eval_monte_carlo(self.md,
                                       n=n + 0.1,
                                       df_det="nom",
                                       seed=101)

        self.assertTrue(gr.df_equal(df_round, df_truth))

        ## Pass-through
        df_pass = gr.eval_monte_carlo(self.md,
                                      n=n,
                                      skip=True,
                                      df_det="nom",
                                      seed=101)

        self.assertTrue(gr.df_equal(df_pass[["x0"]], df_truth[["x0"]]))
Exemple #3
0
    def test_grad_fd(self):
        """Checks the FD code
        """
        ## Accuracy
        df_grad = gr.eval_grad_fd(
            self.model_2d, df_base=self.df_2d_nominal, append=False
        )

        self.assertTrue(np.allclose(df_grad[self.df_2d_grad.columns], self.df_2d_grad))

        ## Subset
        df_grad_sub = gr.eval_grad_fd(
            self.model_2d, df_base=self.df_2d_nominal, var=["x"], append=False
        )

        self.assertTrue(set(df_grad_sub.columns) == set(["Df_Dx", "Dg_Dx"]))

        ## Flags
        md_test = (
            gr.Model()
            >> gr.cp_function(fun=lambda x: x[0] + x[1] ** 2, var=2, out=1)
            >> gr.cp_marginals(x0={"dist": "norm", "loc": 0, "scale": 1})
        )
        df_base = pd.DataFrame(dict(x0=[0, 1], x1=[0, 1]))
        ## Multiple base points
        df_true = pd.DataFrame(dict(Dy0_Dx0=[1, 1], Dy0_Dx1=[0, 2]))

        df_rand = gr.eval_grad_fd(md_test, df_base=df_base, var="rand", append=False)
        self.assertTrue(gr.df_equal(df_true[["Dy0_Dx0"]], df_rand, close=True))

        df_det = gr.eval_grad_fd(md_test, df_base=df_base, var="det", append=False)
        self.assertTrue(gr.df_equal(df_true[["Dy0_Dx1"]], df_det, close=True))
Exemple #4
0
    def test_outer(self):
        df = pd.DataFrame(dict(x=[1, 2]))
        df_outer = pd.DataFrame(dict(y=[3, 4]))

        df_true = pd.DataFrame(dict(x=[1, 2, 1, 2], y=[3, 3, 4, 4]))

        df_res = gr.tran_outer(df, df_outer)
        df_piped = df >> gr.tf_outer(df_outer)

        pd.testing.assert_frame_equal(
            df_true,
            df_res,
            check_exact=False,
            check_dtype=False,
            check_column_type=False,
        )
        pd.testing.assert_frame_equal(
            df_piped,
            df_res,
            check_exact=False,
            check_dtype=False,
            check_column_type=False,
        )

        # Empty cases
        gr.df_equal(
            df,
            gr.tran_outer(pd.DataFrame(), df_outer=df)
        )
        gr.df_equal(
            df,
            gr.tran_outer(df, df_outer=pd.DataFrame())
        )
Exemple #5
0
    def test_pca(self):
        df_test = pd.DataFrame(dict(x0=[1, 2, 3], x1=[1, 2, 3]))
        df_offset = pd.DataFrame(dict(x0=[1, 2, 3], x1=[3, 4, 5]))
        df_scaled = pd.DataFrame(dict(x0=[1, 2, 3], x1=[1, 3, 5]))

        df_true = pd.DataFrame(
            dict(
                lam=[2, 0],
                x0=[1 / np.sqrt(2), 1 / np.sqrt(2)],
                x1=[1 / np.sqrt(2), -1 / np.sqrt(2)],
            )
        )

        ## Check correctness
        df_pca = df_test >> gr.tf_pca()

        self.assertTrue(np.allclose(df_true.lam, df_pca.lam))

        ## Offset data should not affect results
        df_pca_off = df_offset >> gr.tf_pca()
        self.assertTrue(gr.df_equal(df_pca, df_pca_off))

        ## Check standardized form
        df_pca_scaled = df_test >> gr.tf_pca(standardize=True)
        self.assertTrue(gr.df_equal(df_pca, df_pca_scaled))
Exemple #6
0
    def test_conservative(self):
        ## Accuracy
        df_res = gr.eval_conservative(self.model_2d, quantiles=[0.1, 0.1])

        self.assertTrue(gr.df_equal(self.df_2d_qe, df_res, close=True))

        ## Invariant checks
        self.inv_test.md_arg(gr.eval_conservative, df_arg="df_det")
        self.inv_test.df_arg(gr.eval_conservative,
                             df_arg="df_det",
                             shortcut=True,
                             acc_none="var_det")

        ## Repeat scalar value
        self.assertTrue(
            gr.df_equal(
                self.df_2d_qe,
                gr.eval_conservative(self.model_2d, quantiles=0.1),
                close=True,
            ))

        ## Pass-through
        self.assertTrue(
            gr.df_equal(
                self.df_2d_qe.drop(["f", "g"], axis=1),
                gr.eval_conservative(self.model_2d, quantiles=0.1, skip=True),
                close=True,
            ))
Exemple #7
0
    def test_conversion(self):
        df_pr_true = pd.DataFrame(dict(x=[0.5], y=[0.5]))
        df_sp_true = pd.DataFrame(dict(x=[0.0], y=[0.0]))

        df_pr_res = self.density.sample2pr(df_sp_true)
        df_sp_res = self.density.pr2sample(df_pr_true)

        self.assertTrue(gr.df_equal(df_pr_true, df_pr_res))
        self.assertTrue(gr.df_equal(df_sp_true, df_sp_res))
Exemple #8
0
    def test_explode(self):
        df_base = gr.df_make(x=[1, 2], y=[[3, 4], [5, 6]])
        df_str = gr.df_make(x=[1, 2], y=[["3", "4"], ["5", "6"]])
        df_true = gr.df_make(x=[1, 1, 2, 2], y=[3, 4, 5, 6])

        df_res = df_base >> gr.tf_explode(X.y)
        df_res_s = df_base >> gr.tf_explode(X.y, convert=True)

        self.assertTrue(gr.df_equal(df_true, df_res, close=True))
        self.assertTrue(gr.df_equal(df_true, df_res_s, close=True))
Exemple #9
0
    def test_dropna(self):
        df = gr.df_make(x=[1.0, 2.0, 3.0],
                        y=[1.0, np.nan, 3.0],
                        z=[1.0, 2.0, np.nan])

        df_true_default = gr.df_make(x=[1.0], y=[1.0], z=[1.0])
        df_true_y = gr.df_make(x=[1.0, 3.0], y=[1.0, 3.0], z=[1.0, np.nan])

        df_res_default = df >> gr.tf_dropna()
        df_res_y = df >> gr.tf_dropna(subset=["y"])

        self.assertTrue(gr.df_equal(df_true_default, df_res_default))
        self.assertTrue(gr.df_equal(df_true_y, df_res_y))
Exemple #10
0
    def test_nominal(self):
        """Checks the nominal evaluation is accurate
        """
        df_res = gr.eval_nominal(self.model_2d)

        ## Accurate
        self.assertTrue(gr.df_equal(self.df_2d_nominal, df_res))

        ## Pass-through
        self.assertTrue(
            gr.df_equal(
                self.df_2d_nominal.drop(["f", "g"], axis=1),
                gr.eval_nominal(self.model_2d, skip=True),
            ))
Exemple #11
0
    def test_transforms(self):
        ## Setup
        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]]))

        md = (
            gr.Model() >> gr.cp_marginals(x=dict(dist="norm", loc=0, scale=1),
                                          y=dict(dist="norm", loc=0, scale=1))
            >> gr.cp_copula_gaussian(df_corr=df_corr))

        ## Copula and marginals have same var_rand order
        self.assertTrue(
            list(md.density.marginals) == md.density.copula.var_rand)

        ## Transforms invariant
        z = np.array([0, 0])
        x = md.z2x(z)
        zp = md.x2z(x)

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

        df_z = gr.df_make(x=0.0, y=0.0)
        df_x = md.norm2rand(df_z)
        df_zp = md.rand2norm(df_x)

        self.assertTrue(gr.df_equal(df_z, df_zp))

        ## Jacobian accurate
        dxdz_fd = np.zeros((2, 2))
        dxdz_fd[0, :] = (md.z2x(z + np.array([h, 0])) - md.z2x(z)) / h
        dxdz_fd[1, :] = (md.z2x(z + np.array([0, h])) - md.z2x(z)) / h
        dxdz_p = md.dxdz(z)

        self.assertTrue(np.allclose(dxdz_fd, dxdz_p))
Exemple #12
0
    def test_gp(self):
        ## Fit routine creates usable model
        md_fit = fit.fit_gp(self.df_smooth, md=self.md_smooth)
        df_res = gr.eval_df(md_fit, self.df_smooth[self.md_smooth.var])

        ## GP provides std estimates
        self.assertTrue("y_sd" in df_res.columns)

        ## GP is an interpolation
        self.assertTrue(
            gr.df_equal(
                df_res[["x", "y_mean",
                        "z_mean"]].rename({
                            "y_mean": "y",
                            "z_mean": "z"
                        },
                                          axis=1),
                self.df_smooth,
                close=True,
            ))

        ## Fit copies model data
        self.assertTrue(set(md_fit.var) == set(self.md_smooth.var))
        self.assertTrue(
            set(md_fit.out) == set(
                map(lambda s: s + "_mean", self.md_smooth.out)).union(
                    set(map(lambda s: s + "_sd", self.md_smooth.out))))
Exemple #13
0
    def test_lolo(self):
        ## Fit routine creates usable model
        md_fit = fit.fit_lolo(
            self.df_tree,
            md=self.md_tree,
            max_depth=1,  # True tree is a stump
            seed=102,
        )
        df_res = gr.eval_df(md_fit, self.df_tree[self.md_tree.var])

        ## lolo seems to interpolate middle values; check ends only
        self.assertTrue(
            gr.df_equal(
                df_res[["y", "z"]].iloc[[0, 1, -2, -1]],
                self.df_tree[["y", "z"]].iloc[[0, 1, -2, -1]],
                close=True,
                precision=1,
            )
        )

        ## Fit copies model data, plus predictive std
        self.assertTrue(set(md_fit.var) == set(self.md_tree.var))
        self.assertTrue(
            set(md_fit.out)
            == set(self.md_tree.out + list(map(lambda s: s + "_std", self.md_tree.out)))
        )
Exemple #14
0
    def test_rf(self):
        ## Fit routine creates usable model
        md_fit = fit.fit_rf(
            self.df_tree,
            md=self.md_tree,
            max_depth=1,  # True tree is a stump
            seed=101,
        )
        df_res = gr.eval_df(md_fit, self.df_tree[self.md_tree.var])

        ## RF can approximately recover a tree; check ends only
        self.assertTrue(
            gr.df_equal(
                df_res[["y_mean", "z_mean"]].iloc[[0, 1, -2, -1]],
                self.df_tree[["y", "z"]].iloc[[0, 1, -2, -1]] >> gr.tf_rename(
                    y_mean="y", z_mean="z"),
                close=True,
                precision=1,
            ))

        ## Fit copies model data
        self.assertTrue(set(md_fit.var) == set(self.md_tree.var))
        self.assertTrue(
            set(md_fit.out) == set(map(lambda s: s + "_mean",
                                       self.md_tree.out)))
Exemple #15
0
    def test_ev_nominal(self):
        """Check ev_nominal()
        """
        df_res = gr.eval_nominal(self.model_default, df_det="nom")

        self.assertTrue(
            gr.df_equal(df_res,
                        self.model_default >> gr.ev_nominal(df_det="nom")))
Exemple #16
0
    def test_ev_df(self):
        """Check ev_df()
        """
        df_res = gr.eval_df(self.model_default, df=self.df_test)

        self.assertTrue(
            gr.df_equal(df_res,
                        self.model_default >> gr.ev_df(df=self.df_test)))
Exemple #17
0
    def test_ev_sinews(self):
        """Check ev_sinews()
        """
        df_res = gr.eval_sinews(self.model_default, seed=101, df_det="nom")

        self.assertTrue(
            gr.df_equal(
                df_res,
                self.model_default >> gr.ev_sinews(seed=101, df_det="nom")))
Exemple #18
0
    def test_ev_grad_fd(self):
        """Check ev_grad_fd()
        """
        df_res = gr.eval_grad_fd(self.model_default, df_base=self.df_test)

        self.assertTrue(
            gr.df_equal(
                df_res,
                self.model_default >> gr.ev_grad_fd(df_base=self.df_test)))
Exemple #19
0
    def test_nominal(self):
        """Checks the implementation of nominal values"""
        md = gr.Model() >> gr.cp_bounds(
            x0=[-1, +1], x1=[0.1, np.Inf], x2=[-np.Inf, -0.1],
        )
        df_true = gr.df_make(x0=0.0, x1=+0.1, x2=-0.1)
        df_res = gr.eval_nominal(md, df_det="nom", skip=True)

        self.assertTrue(gr.df_equal(df_res, df_true))
Exemple #20
0
    def test_drop_out(self):
        """Checks that output column names are properly dropped"""
        md = gr.Model() >> gr.cp_function(lambda x: x[0] + 1, var=1, out=1)
        df_in = gr.df_make(x0=[0, 1, 2], y0=[0, 1, 2])
        df_true = gr.df_make(x0=[0, 1, 2], y0=[1, 2, 3])

        df_res = md >> gr.ev_df(df=df_in)

        self.assertTrue(gr.df_equal(df_res, df_true, close=True))
Exemple #21
0
    def test_tran_md(self):
        md = models.make_test()

        ## Check for identical responses
        df = gr.df_make(x0=1, x1=1, x2=1)
        df_ev = gr.eval_df(md, df=df)
        df_tf = gr.tran_md(df, md=md)

        self.assertTrue(gr.df_equal(df_ev, df_tf))
Exemple #22
0
    def test_tran_poly(self):
        df = gr.df_make(x=[0.0, 1.0, 0.0], y=[0.0, 0.0, 1.0], z=[1.0, 2.0, 3.0],)
        df_true = df.copy()
        df_true["1"] = [1.0, 1.0, 1.0]
        df_true["x^2"] = [0.0, 1.0, 0.0]
        df_true["x y"] = [0.0, 0.0, 0.0]
        df_true["y^2"] = [0.0, 0.0, 1.0]

        df_res = gr.tran_poly(df, var=["x", "y"], degree=2, keep=True)
        self.assertTrue(gr.df_equal(df_true, df_res[df_true.columns]))
Exemple #23
0
    def test_lhs(self):
        ## Accurate
        n = 2
        df_res = ev.eval_lhs(self.md_2d, n=n, df_det="nom", seed=101)

        np.random.seed(101)
        df_truth = pd.DataFrame(data=lhs(2, samples=n), columns=["x0", "x1"])
        df_truth["y0"] = df_truth["x0"]

        self.assertTrue(gr.df_equal(df_res, df_truth))

        ## Rounding
        df_round = ev.eval_lhs(self.md_2d, n=n + 0.1, df_det="nom", seed=101)

        self.assertTrue(gr.df_equal(df_round, df_truth))

        ## Pass-through
        df_pass = ev.eval_lhs(self.md_2d, n=n, skip=True, df_det="nom", seed=101)

        self.assertTrue(gr.df_equal(df_pass, df_truth[["x0", "x1"]]))
Exemple #24
0
    def test_nominal(self):
        """Checks the nominal evaluation is accurate
        """
        df_res = gr.eval_nominal(self.model_2d)

        ## Accurate
        self.assertTrue(gr.df_equal(self.df_2d_nominal, df_res))

        ## Invariant checks
        self.inv_test.md_arg(gr.eval_nominal, df_arg="df_det")
        self.inv_test.df_arg(gr.eval_nominal,
                             df_arg="df_det",
                             shortcut=True,
                             acc_none="var_det")

        ## Pass-through
        self.assertTrue(
            gr.df_equal(
                self.df_2d_nominal.drop(["f", "g"], axis=1),
                gr.eval_nominal(self.model_2d, skip=True),
            ))
Exemple #25
0
    def test_conservative(self):
        ## Accuracy
        df_res = gr.eval_conservative(self.model_2d, quantiles=[0.1, 0.1])

        self.assertTrue(gr.df_equal(self.df_2d_qe, df_res, close=True))

        ## Repeat scalar value
        self.assertTrue(
            gr.df_equal(
                self.df_2d_qe,
                gr.eval_conservative(self.model_2d, quantiles=0.1),
                close=True,
            ))

        ## Pass-through
        self.assertTrue(
            gr.df_equal(
                self.df_2d_qe.drop(["f", "g"], axis=1),
                gr.eval_conservative(self.model_2d, quantiles=0.1, skip=True),
                close=True,
            ))
Exemple #26
0
    def test_df_make(self):
        # Check correctness
        df_true = pd.DataFrame(dict(x=[0, 1], y=[0, 0], z=[1, 1]))
        df_res = gr.df_make(x=[0, 1], y=[0], z=1)

        self.assertTrue(gr.df_equal(df_true, df_res))

        # Check for mismatch
        with self.assertRaises(ValueError):
            gr.df_make(x=[1, 2, 3], y=[1, 2])

        # Catch an intention operator
        with self.assertRaises(ValueError):
            gr.df_make(y=DF.x)
Exemple #27
0
    def test_gp(self):
        ## Fit routine creates usable model
        md_fit = fit.fit_gp(self.df_smooth, md=self.md_smooth)
        df_res = gr.eval_df(md_fit, self.df_smooth[self.md_smooth.var])

        ## GP provides std estimates
        # self.assertTrue("y_std" in df_res.columns)

        ## GP is an interpolation
        self.assertTrue(gr.df_equal(df_res, self.df_smooth, close=True))

        ## Fit copies model data
        self.assertTrue(set(md_fit.var) == set(self.md_smooth.var))
        self.assertTrue(set(md_fit.out) == set(self.md_smooth.out))
Exemple #28
0
    def test_bootstrap(self):
        df_stang = data.df_stang
        df_stang._meta = "foo"

        def tran_stats(df):
            val = df.select_dtypes(include="number").values

            means = np.mean(val, axis=0)
            stds = np.std(val, axis=0)

            # Check metadata propagation
            self.assertTrue(df._meta == "foo")

            return pd.DataFrame(
                data={
                    "var": df.select_dtypes(include="number").columns,
                    "mean": means,
                    "std": stds,
                })

        df_res = gr.tran_bootstrap(df_stang,
                                   tran=tran_stats,
                                   n_boot=3e0,
                                   n_sub=3e0,
                                   seed=101)

        df_sel = gr.tran_bootstrap(df_stang,
                                   tran=tran_stats,
                                   n_boot=3e0,
                                   n_sub=3e0,
                                   seed=101,
                                   col_sel=["mean"])

        df_piped = df_stang >> gr.tf_bootstrap(
            tran=tran_stats, n_boot=3e0, n_sub=3e0, seed=101)

        ## Test output shape
        self.assertTrue(
            set(df_res.columns) == set([
                "var", "mean", "mean_lo", "mean_up", "std", "std_lo", "std_up"
            ]))
        self.assertTrue(df_res.shape[0] == 4)

        self.assertTrue(
            set(df_sel.columns) == set(
                ["var", "mean", "mean_lo", "mean_up", "std"]))
        self.assertTrue(df_sel.shape[0] == 4)

        ## Test pipe
        self.assertTrue(gr.df_equal(df_res, df_piped))
Exemple #29
0
    def test_ria(self):
        ## Test accuracy
        df_res = self.md >> gr.ev_form_ria(df_det="nom", limits=["g"])
        self.assertTrue(np.allclose(df_res["beta_g"], [self.beta_true], atol=1e-3))

        ## Test MPP mapped correctly
        df_mpp = self.md_log >> gr.ev_form_ria(df_det="nom", limits=["g"])
        self.assertTrue(
            gr.df_equal(df_mpp, self.df_mpp[["x", "y", "beta_g"]], close=True)
        )

        ## Test flatten
        df_beam = self.md_beam >> gr.ev_form_ria(
            df_det="nom", limits=["g_stress", "g_disp"], append=False
        )
        self.assertTrue(df_beam.shape[0] == 1)
Exemple #30
0
    def test_pma(self):
        ## Test accuracy
        df_res = self.md >> gr.ev_form_pma(df_det="nom", betas=dict(g=self.beta_true))
        self.assertTrue(np.allclose(df_res["g"], [0], atol=1e-3))

        ## Test MPP mapped correctly
        df_mpp = self.md_log >> gr.ev_form_pma(df_det="nom", betas=dict(g=1.0))
        self.assertTrue(
            gr.df_equal(df_mpp, self.df_mpp[["x", "y", "g"]], close=True)
        )

        ## Test flatten
        df_beam = self.md_beam >> gr.ev_form_pma(
            df_det="nom", betas={"g_stress": 3, "g_disp": 3}, append=False,
        )
        self.assertTrue(df_beam.shape[0] == 1)