Пример #1
0
    def setUp(self):
        self.md = (gr.Model() >> gr.cp_function(
            fun=lambda x: x, var=1, out=1, runtime=1) >>
                   gr.cp_marginals(x0={
                       "dist": "uniform",
                       "loc": 0,
                       "scale": 1
                   }) >> gr.cp_copula_independence())

        self.md_2d = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[0], var=2, out=1) >> gr.cp_marginals(
                x0={
                    "dist": "uniform",
                    "loc": 0,
                    "scale": 1
                },
                x1={
                    "dist": "uniform",
                    "loc": 0,
                    "scale": 1
                },
            ) >> gr.cp_copula_independence())

        self.md_mixed = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[0] + x[1], var=2,
            out=1) >> gr.cp_bounds(x0=(-1, +1)) >> gr.cp_marginals(x1={
                "dist": "uniform",
                "loc": 0,
                "scale": 1
            }, ) >> gr.cp_copula_independence())
Пример #2
0
    def setUp(self):
        ## Smooth model
        self.md_smooth = (gr.Model() >> gr.cp_function(
            fun=lambda x: [x, x + 1], var=["x"], out=["y", "z"]) >>
                          gr.cp_marginals(x={
                              "dist": "uniform",
                              "loc": 0,
                              "scale": 2
                          }) >> gr.cp_copula_independence())

        self.df_smooth = self.md_smooth >> gr.ev_df(
            df=pd.DataFrame(dict(x=[0, 1, 2])))

        ## Tree model
        self.md_tree = (gr.Model() >> gr.cp_function(
            fun=lambda x: [0, x < 5], var=["x"], out=["y", "z"]) >>
                        gr.cp_marginals(x={
                            "dist": "uniform",
                            "loc": 0,
                            "scale": 2
                        }) >> gr.cp_copula_independence())

        self.df_tree = self.md_tree >> gr.ev_df(
            df=pd.DataFrame(dict(x=np.linspace(0, 10, num=8))))

        ## Cluster model
        self.df_cluster = pd.DataFrame(
            dict(
                x=[0.1, 0.2, 0.3, 0.4, 1.1, 1.2, 1.3, 1.4],
                y=[0.3, 0.2, 0.1, 0.0, 1.3, 1.2, 1.1, 1.0],
                c=[0, 0, 0, 0, 1, 1, 1, 1],
            ))
Пример #3
0
 def setUp(self):
     self.md = (gr.Model() >> gr.cp_function(
         fun=lambda x: x,
         var=["x"],
         out=["y"],
     ) >> gr.cp_marginals(x=dict(dist="norm", loc=0, scale=1)) >>
                gr.cp_copula_independence())
Пример #4
0
    def setUp(self):
        ## Linear limit state w/ MPP off initial guess
        self.beta_true = 3
        self.md = (
            gr.Model()
            >> gr.cp_function(
                fun=lambda x: self.beta_true * 2 - x[0] - np.sqrt(3) * x[1],
                var=2,
                out=["g"],
            )
            >> gr.cp_marginals(
                x0=dict(dist="norm", loc=0, scale=1, sign=1),
                x1=dict(dist="norm", loc=0, scale=1, sign=1),
            )
            >> gr.cp_copula_independence()
        )

        ## Linear limit state w/ lognormal marginals
        self.md_log = (
            gr.Model()
            >> gr.cp_vec_function(
                fun=lambda df: gr.df_make(
                    g=gr.exp(gr.sqrt(2) * 1) - df.x * df.y
                ),
                var=["x", "y"],
                out=["g"]
            )
            >> gr.cp_marginals(
                x=dict(dist="lognorm", loc=0, scale=1, s=1),
                y=dict(dist="lognorm", loc=0, scale=1, s=1),
            )
            >> gr.cp_copula_independence()
        )
        self.df_mpp = gr.df_make(
            x=gr.exp(gr.sqrt(2)/2),
            y=gr.exp(gr.sqrt(2)/2),
            beta_g=1.0,
            g=0.0,
        )

        ## Cantilever beam for flatten test
        self.md_beam = models.make_cantilever_beam()
Пример #5
0
    def setUp(self):
        self.md = models.make_test()

        self.md_mixed = (
            gr.Model()
            >> gr.cp_function(fun=lambda x: x[0], var=3, out=1)
            >> gr.cp_bounds(x2=(0, 1))
            >> gr.cp_marginals(
                x0={"dist": "uniform", "loc": 0, "scale": 1},
                x1={"dist": "uniform", "loc": 0, "scale": 1},
            )
            >> gr.cp_copula_independence()
        )
Пример #6
0
 def setUp(self):
     ## Linear limit state w/ MPP off initial guess
     self.beta_true = 3
     self.md = (
         gr.Model()
         >> gr.cp_function(
             fun=lambda x: self.beta_true * 2 - x[0] - np.sqrt(3) * x[1],
             var=2,
             out=["g"],
         )
         >> gr.cp_marginals(
             x0=dict(dist="norm", loc=0, scale=1, sign=1),
             x1=dict(dist="norm", loc=0, scale=1, sign=1),
         )
         >> gr.cp_copula_independence()
     )
Пример #7
0
    def test_comp_model(self):
        """Test model composition"""
        md_inner = (
            gr.Model("inner")
            >> gr.cp_function(fun=lambda x: x[0] + x[1], var=2, out=1)
            >> gr.cp_marginals(x0=dict(dist="norm", loc=0, scale=1))
            >> gr.cp_copula_independence()
        )

        ## Deterministic composition
        md_det = gr.Model("outer_det") >> gr.cp_md_det(md=md_inner)

        self.assertTrue(set(md_det.var) == {"x0", "x1"})
        self.assertTrue(md_det.out == ["y0"])
        gr.eval_df(md_det, df=gr.df_make(x0=0, x1=0))

        ## Deterministic composition
        md_sample = gr.Model("outer_det") >> gr.cp_md_sample(
            md=md_inner, param=dict(x0=("loc", "scale"))
        )

        self.assertTrue(set(md_sample.var) == {"x0_loc", "x0_scale", "x1"})
        self.assertTrue(set(md_sample.out) == {"y0"})
        gr.eval_df(md_sample, df=gr.df_make(x0_loc=0, x0_scale=1, x1=0))
Пример #8
0
    def test_nls(self):
        ## Ground-truth model
        c_true = 2
        a_true = 1

        md_true = (gr.Model() >> gr.cp_function(
            fun=lambda x: a_true * np.exp(x[0] * c_true) + x[1],
            var=["x", "epsilon"],
            out=["y"],
        ) >> gr.cp_marginals(epsilon={
            "dist": "norm",
            "loc": 0,
            "scale": 0.5
        }) >> gr.cp_copula_independence())
        df_data = md_true >> gr.ev_monte_carlo(
            n=5, seed=101, df_det=gr.df_make(x=[0, 1, 2, 3, 4]))

        ## Model to fit
        md_param = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] * np.exp(x[0] * x[1]),
            var=["x", "c", "a"],
            out=["y"]) >> gr.cp_bounds(c=[0, 4], a=[0.1, 2.0]))

        ## Fit the model
        md_fit = df_data >> gr.ft_nls(
            md=md_param,
            verbose=False,
            uq_method="linpool",
        )

        ## Unidentifiable model throws warning
        # -------------------------
        md_unidet = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] / x[3] * np.exp(x[0] * x[1]),
            var=["x", "c", "a", "z"],
            out=["y"],
        ) >> gr.cp_bounds(c=[0, 4], a=[0.1, 2.0], z=[0, 1]))
        with self.assertWarns(RuntimeWarning):
            gr.fit_nls(
                df_data,
                md=md_unidet,
                uq_method="linpool",
            )

        ## True parameters in wide confidence region
        # -------------------------
        alpha = 1e-3
        self.assertTrue(
            (md_fit.density.marginals["c"].q(alpha / 2) <= c_true)
            and (c_true <= md_fit.density.marginals["c"].q(1 - alpha / 2)))

        self.assertTrue(
            (md_fit.density.marginals["a"].q(alpha / 2) <= a_true)
            and (a_true <= md_fit.density.marginals["a"].q(1 - alpha / 2)))

        ## Model with fixed parameter
        # -------------------------
        md_fixed = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] * np.exp(x[0] * x[1]),
            var=["x", "c", "a"],
            out=["y"]) >> gr.cp_bounds(c=[0, 4], a=[1, 1]))
        md_fit_fixed = df_data >> gr.ft_nls(
            md=md_fixed, verbose=False, uq_method="linpool")

        # Test that fixed model can evaluate successfully
        gr.eval_monte_carlo(md_fit_fixed, n=1, df_det="nom")

        ## Trajectory model
        # -------------------------
        md_base = models.make_trajectory_linear()
        md_fit = data.df_trajectory_windowed >> gr.ft_nls(
            md=md_base, method="SLSQP", tol=1e-3)
        df_tmp = md_fit >> gr.ev_nominal(df_det="nom")
Пример #9
0
    def test_nls(self):
        ## Ground-truth model
        c_true = 2
        a_true = 1

        md_true = (gr.Model() >> gr.cp_function(
            fun=lambda x: a_true * np.exp(x[0] * c_true) + x[1],
            var=["x", "epsilon"],
            out=["y"],
        ) >> gr.cp_marginals(epsilon={
            "dist": "norm",
            "loc": 0,
            "scale": 0.5
        }) >> gr.cp_copula_independence())
        df_data = md_true >> gr.ev_sample(
            n=5, seed=101, df_det=gr.df_make(x=[0, 1, 2, 3, 4]))

        ## Model to fit
        md_param = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] * np.exp(x[0] * x[1]),
            var=["x", "c", "a"],
            out=["y"]) >> gr.cp_bounds(c=[0, 4], a=[0.1, 2.0]))

        ## Fit the model
        md_fit = df_data >> gr.ft_nls(
            md=md_param,
            verbose=False,
            uq_method="linpool",
        )

        ## Unidentifiable model throws warning
        # -------------------------
        md_unidet = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] / x[3] * np.exp(x[0] * x[1]),
            var=["x", "c", "a", "z"],
            out=["y"],
        ) >> gr.cp_bounds(c=[0, 4], a=[0.1, 2.0], z=[0, 1]))
        with self.assertWarns(RuntimeWarning):
            gr.fit_nls(
                df_data,
                md=md_unidet,
                uq_method="linpool",
            )

        ## True parameters in wide confidence region
        # -------------------------
        alpha = 1e-3
        self.assertTrue(
            (md_fit.density.marginals["c"].q(alpha / 2) <= c_true)
            and (c_true <= md_fit.density.marginals["c"].q(1 - alpha / 2)))

        self.assertTrue(
            (md_fit.density.marginals["a"].q(alpha / 2) <= a_true)
            and (a_true <= md_fit.density.marginals["a"].q(1 - alpha / 2)))

        ## Model with fixed parameter
        # -------------------------
        md_fixed = (gr.Model() >> gr.cp_function(
            fun=lambda x: x[2] * np.exp(x[0] * x[1]),
            var=["x", "c", "a"],
            out=["y"]) >> gr.cp_bounds(c=[0, 4], a=[1, 1]))
        md_fit_fixed = df_data >> gr.ft_nls(
            md=md_fixed, verbose=False, uq_method="linpool")

        # Test that fixed model can evaluate successfully
        gr.eval_sample(md_fit_fixed, n=1, df_det="nom")

        ## Trajectory model
        # -------------------------
        md_base = models.make_trajectory_linear()
        md_fit = data.df_trajectory_windowed >> gr.ft_nls(
            md=md_base, method="SLSQP", tol=1e-3)
        df_tmp = md_fit >> gr.ev_nominal(df_det="nom")

        ## Select output for fitting
        # -------------------------
        # Split model has inconsistent "true" parameter value
        md_split = (gr.Model("Split") >> gr.cp_vec_function(
            fun=lambda df: gr.df_make(
                f=1 * df.c * df.x,
                g=2 * df.c * df.x,
            ),
            var=["c", "x"],
            out=["f", "g"],
        ) >> gr.cp_bounds(
            x=(-1, +1),
            c=(-1, +1),
        ))

        df_split = (gr.df_make(x=gr.linspace(-1, +1, 100)) >> gr.tf_mutate(
            f=X.x, g=X.x))

        # Fitting both outputs: cannot achieve mse ~= 0
        df_both = (df_split >> gr.ft_nls(md_split, out=["f", "g"]) >>
                   gr.ev_df(df_split >> gr.tf_rename(f_t=X.f, g_t=X.g)) >>
                   gr.tf_summarize(
                       mse_f=gr.mse(X.f, X.f_t),
                       mse_g=gr.mse(X.g, X.g_t),
                   ))
        self.assertTrue(df_both.mse_f[0] > 0)
        self.assertTrue(df_both.mse_g[0] > 0)

        # Fitting "f" only
        df_f = (df_split >> gr.ft_nls(md_split, out=["f"]) >>
                gr.ev_df(df_split >> gr.tf_rename(f_t=X.f, g_t=X.g)) >>
                gr.tf_summarize(
                    mse_f=gr.mse(X.f, X.f_t),
                    mse_g=gr.mse(X.g, X.g_t),
                ))
        self.assertTrue(df_f.mse_f[0] < 1e-16)
        self.assertTrue(df_f.mse_g[0] > 0)

        # Fitting "g" only
        df_g = (df_split >> gr.ft_nls(md_split, out=["g"]) >>
                gr.ev_df(df_split >> gr.tf_rename(f_t=X.f, g_t=X.g)) >>
                gr.tf_summarize(
                    mse_f=gr.mse(X.f, X.f_t),
                    mse_g=gr.mse(X.g, X.g_t),
                ))
        self.assertTrue(df_g.mse_f[0] > 0)
        self.assertTrue(df_g.mse_g[0] < 1e-16)