示例#1
0
def get_data(nobs):
    cop_f = FrankCopula(theta=2)
    cd_f = CopulaDistribution(cop_f, [stats.norm, stats.norm])
    # np.random.seed(98645713)
    # at some seeds, parameters atol-differ from true
    # TODO: setting seed doesn't work for copula,
    # copula creates new randomly initialized random state, see #7650
    rng = np.random.RandomState(98645713)
    rvs = cd_f.rvs(nobs, random_state=rng)
    assert_allclose(rvs.mean(0), [-0.02936002, 0.06658304], atol=1e-7)
    return rvs
示例#2
0
    def setup_class(cls):
        grid = dt._Grid([91, 101])

        cop_tr = tra.TransfFrank
        args = (2, )
        ca = ArchimedeanCopula(cop_tr())
        distr1 = stats.beta(4, 3)
        distr2 = stats.beta(4, 4)  # (5, 2)
        cad = CopulaDistribution(ca, [distr1, distr2], cop_args=args)
        cdfv = cad.cdf(grid.x_flat, args)
        cdf_g = cdfv.reshape(grid.k_grid)

        cls.grid = grid
        cls.cdfv = cdfv
        cls.distr = cad
        cls.bpd = BernsteinDistribution(cdf_g)
示例#3
0
    def test0(self):
        # test with fixed copula params
        cop = getattr(self, "copula_fixed", None)
        if cop is None:
            # skip test if not yet available
            return
        args = self.cop_args

        cev = CopulaDistribution(cop, [stats.norm, stats.norm], cop_args=args)
        k_marg = 2
        mod = CopulaModel(cev, data_ev + [0.5, -0.1], k_params=0 + k_marg)

        # TODO: patching for now
        mod.k_copparams = 0
        mod.df_resid = len(mod.endog) - mod.nparams
        mod.df_model = mod.nparams - 0
        res = mod.fit(start_params=[0.5, -0.1], method="bfgs")
        # the following fails in TestEVAsymLogistic with nan loglike
        # res = mod.fit(method="newton", start_params=res.params)

        assert mod.nparams == 0 + k_marg
        assert res.nobs == len(mod.endog)
        assert_allclose(res.params, [0.5, -0.1], atol=0.2)
        res.summary()
        assert res.mle_retvals["converged"]
        assert not np.isnan(res.bse).any()
示例#4
0
def test_bernstein_distribution_2d():
    grid = dt._Grid([51, 51])

    cop_tr = tra.TransfFrank
    args = (2, )
    ca = ArchimedeanCopula(cop_tr())
    distr1 = stats.uniform
    distr2 = stats.uniform
    cad = CopulaDistribution(ca, [distr1, distr2], cop_args=args)
    cdfv = cad.cdf(grid.x_flat, args)
    cdf_g = cdfv.reshape(grid.k_grid)

    bpd = BernsteinDistribution(cdf_g)

    cdf_bp = bpd.cdf(grid.x_flat)
    assert_allclose(cdf_bp, cdfv, atol=0.005)
    assert_array_less(np.median(np.abs(cdf_bp - cdfv)), 0.001)

    grid_eps = dt._Grid([51, 51], eps=1e-8)
    pdfv = cad.pdf(grid_eps.x_flat)
    pdf_bp = bpd.pdf(grid_eps.x_flat)
    assert_allclose(pdf_bp, pdfv, atol=0.01, rtol=0.04)
    assert_array_less(np.median(np.abs(pdf_bp - pdfv)), 0.05)

    # check marginal cdfs
    # get marginal cdf
    xx = np.column_stack((np.linspace(0, 1, 5), np.ones(5)))
    cdf_m1 = bpd.cdf(xx)
    assert_allclose(cdf_m1, xx[:, 0], atol=1e-13)
    xx = np.column_stack((np.ones(5), np.linspace(0, 1, 5)))
    cdf_m2 = bpd.cdf(xx)
    assert_allclose(cdf_m2, xx[:, 1], atol=1e-13)

    xx_ = np.linspace(0, 1, 5)
    xx = xx_[:, None]  # currently requires 2-dim
    bpd_m1 = bpd.get_marginal(0)
    cdf_m1 = bpd_m1.cdf(xx)
    assert_allclose(cdf_m1, xx_, atol=1e-13)
    pdf_m1 = bpd_m1.pdf(xx)
    assert_allclose(pdf_m1, np.ones(len(xx)), atol=1e-13)

    bpd_m = bpd.get_marginal(1)
    cdf_m = bpd_m.cdf(xx)
    assert_allclose(cdf_m, xx_, atol=1e-13)
    pdf_m = bpd_m.pdf(xx)
    assert_allclose(pdf_m, np.ones(len(xx)), atol=1e-13)
示例#5
0
    def test2m(self):
        cop = self.copula
        args = self.cop_args

        cev = CopulaDistribution(cop, [stats.norm, stats.norm], cop_args=None)
        k_marg = 2
        mod = CopulaModel(cev,
                          data_ev + [0.5, -0.1],
                          k_params=self.k_copparams + k_marg)

        # TODO: patching for now
        mod.k_copparams = self.k_copparams
        mod.df_resid = len(mod.endog) - mod.nparams
        mod.df_model = mod.nparams - 0
        res = mod.fit(start_params=list(args) + [0.5, -0.1], method="bfgs")
        # the following fails in TestEVAsymLogistic with nan loglike
        # res = mod.fit(method="newton", start_params=res.params)

        assert mod.nparams == self.k_copparams + k_marg
        assert res.nobs == len(mod.endog)
        assert_allclose(res.params[[-2, -1]], [0.5, -0.1], atol=0.2)
        res.summary()
        assert res.mle_retvals["converged"]
        assert not np.isnan(res.bse).any()
示例#6
0
#
# Generating reproducible random values from copulas required explicitly
# setting the `seed` argument.
# `seed` accepts either an initialized NumPy `Generator` or `RandomState`,
# or any argument acceptable
# to `np.random.default_rng`, e.g., an integer or a sequence of integers.
# This example uses an
# integer.
#
# The singleton `RandomState` that is directly exposed in the `np.random`
# distributions is
# not used, and setting `np.random.seed` has no effect on the values
# generated.

marginals = [stats.gamma(2), stats.norm]
joint_dist = CopulaDistribution(copula=IndependenceCopula(),
                                marginals=marginals)
sample = joint_dist.rvs(512, random_state=20210801)
h = sns.jointplot(x=sample[:, 0], y=sample[:, 1], kind="scatter")
_ = h.set_axis_labels("X1", "X2", fontsize=16)

# Now, above we have expressed the dependency between our variables using
# a copula, we can use this copula to sample a new set of observation with
# the same convenient class.

joint_dist = CopulaDistribution(copula, marginals)
# Use an initialized Generator object
rng = np.random.default_rng([2, 0, 2, 1, 0, 8, 0, 1])
sample = joint_dist.rvs(512, random_state=rng)
h = sns.jointplot(x=sample[:, 0], y=sample[:, 1], kind="scatter")
_ = h.set_axis_labels("X1", "X2", fontsize=16)
示例#7
0
#
# Generating reproducible random values from copulas required explicitly
# setting the `seed` argument.
# `seed` accepts either an initialized NumPy `Generator` or `RandomState`,
# or any argument acceptable
# to `np.random.default_rng`, e.g., an integer or a sequence of integers.
# This example uses an
# integer.
#
# The singleton `RandomState` that is directly exposed in the `np.random`
# distributions is
# not used, and setting `np.random.seed` has no effect on the values
# generated.

marginals = [stats.gamma(2), stats.norm]
joint_dist = CopulaDistribution(marginals=marginals, copula=None)
sample = joint_dist.rvs(512, random_state=20210801)
h = sns.jointplot(x=sample[:, 0], y=sample[:, 1], kind="scatter")
_ = h.set_axis_labels("X1", "X2", fontsize=16)

# Now, above we have expressed the dependency between our variables using
# a copula, we can use this copula to sample a new set of observation with
# the same convenient class.

joint_dist = CopulaDistribution(marginals=marginals, copula=copula)
# Use an initialized Generator object
rng = np.random.default_rng([2, 0, 2, 1, 0, 8, 0, 1])
sample = joint_dist.rvs(512, random_state=rng)
h = sns.jointplot(x=sample[:, 0], y=sample[:, 1], kind="scatter")
_ = h.set_axis_labels("X1", "X2", fontsize=16)