示例#1
0
    def test_skewstudent(self):
        dist = SkewStudent()
        eta, lam = 4.0, .5
        ll1 = dist.loglikelihood(np.array([eta, lam]), self.resids,
                                 self.sigma2)
        # Direct calculation of PDF, then log
        const_c = gamma(
            (eta + 1) / 2) / ((np.pi * (eta - 2))**.5 * gamma(eta / 2))
        const_a = 4 * lam * const_c * (eta - 2) / (eta - 1)
        const_b = (1 + 3 * lam**2 - const_a**2)**.5

        resids = self.resids / self.sigma2**.5
        pow = (-(eta + 1) / 2)
        pdf = const_b * const_c / self.sigma2 ** .5 * \
            (1 + 1 / (eta - 2) *
             ((const_b * resids + const_a) /
              (1 + np.sign(resids + const_a / const_b) * lam)) ** 2) ** pow

        ll2 = np.log(pdf).sum()
        assert_almost_equal(ll1, ll2)

        assert_equal(dist.num_params, 2)

        bounds = dist.bounds(self.resids)
        assert_equal(len(bounds), 2)

        a, b = dist.constraints()
        assert_equal(a.shape, (4, 2))

        k = stats.kurtosis(self.resids, fisher=False)
        sv = max((4.0 * k - 6.0) / (k - 3.0) if k > 3.75 else 12.0, 4.0)
        assert_array_equal(dist.starting_values(self.resids),
                           np.array([sv, 0.]))

        with pytest.raises(ValueError):
            dist.simulate(np.array([1.5, 0.]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([4., 1.5]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([4., -1.5]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([1.5, 1.5]))
示例#2
0
    def test_skewstudent(self):
        dist = SkewStudent()
        eta, lam = 4.0, .5
        ll1 = dist.loglikelihood(np.array([eta, lam]),
                                 self.resids, self.sigma2)
        # Direct calculation of PDF, then log
        const_c = gamma((eta+1)/2) / ((np.pi*(eta-2))**.5 * gamma(eta/2))
        const_a = 4*lam*const_c*(eta-2)/(eta-1)
        const_b = (1 + 3*lam**2 - const_a**2)**.5

        resids = self.resids / self.sigma2 ** .5
        pow = (-(eta + 1) / 2)
        pdf = const_b * const_c / self.sigma2 ** .5 * \
            (1 + 1 / (eta - 2) *
             ((const_b * resids + const_a) /
              (1 + np.sign(resids + const_a / const_b) * lam)) ** 2) ** pow

        ll2 = np.log(pdf).sum()
        assert_almost_equal(ll1, ll2)

        assert_equal(dist.num_params, 2)

        bounds = dist.bounds(self.resids)
        assert_equal(len(bounds), 2)

        a, b = dist.constraints()
        assert_equal(a.shape, (4, 2))

        k = stats.kurtosis(self.resids, fisher=False)
        sv = max((4.0 * k - 6.0) / (k - 3.0) if k > 3.75 else 12.0, 4.0)
        assert_array_equal(dist.starting_values(self.resids),
                           np.array([sv, 0.]))

        with pytest.raises(ValueError):
            dist.simulate(np.array([1.5, 0.]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([4., 1.5]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([4., -1.5]))
        with pytest.raises(ValueError):
            dist.simulate(np.array([1.5, 1.5]))
示例#3
0
    def test_warnings(self):
        garch = GARCH()
        parameters = np.array([0.1, 0.2, 0.8, 4.0])
        studt = StudentsT()
        with warnings.catch_warnings(record=True) as w:
            garch.simulate(parameters, 1000, studt.simulate([4.0]))
            assert_equal(len(w), 1)

        garch = GARCH()
        parameters = np.array([0.1, 0.2, 0.8, 4.0, 0.5])
        skewstud = SkewStudent()
        with warnings.catch_warnings(record=True) as w:
            garch.simulate(parameters, 1000, skewstud.simulate([4.0, 0.5]))
            assert_equal(len(w), 1)

        harch = HARCH(lags=[1, 5, 22])
        parameters = np.array([0.1, 0.2, 0.4, 0.5])
        with warnings.catch_warnings(record=True) as w:
            harch.simulate(parameters, 1000, studt.simulate([4.0]))
            assert_equal(len(w), 1)
示例#4
0
    def test_warnings(self):
        garch = GARCH()
        parameters = np.array([0.1, 0.2, 0.8, 4.0])
        studt = StudentsT()
        with warnings.catch_warnings(record=True) as w:
            garch.simulate(parameters, 1000, studt.simulate([4.0]))
            assert_equal(len(w), 1)

        garch = GARCH()
        parameters = np.array([0.1, 0.2, 0.8, 4.0, 0.5])
        skewstud = SkewStudent()
        with warnings.catch_warnings(record=True) as w:
            garch.simulate(parameters, 1000, skewstud.simulate([4.0, 0.5]))
            assert_equal(len(w), 1)

        harch = HARCH(lags=[1, 5, 22])
        parameters = np.array([0.1, 0.2, 0.4, 0.5])
        with warnings.catch_warnings(record=True) as w:
            harch.simulate(parameters, 1000, studt.simulate([4.0]))
            assert_equal(len(w), 1)
示例#5
0
 def test_warnings_nonstationary_garch(self):
     garch = GARCH()
     parameters = np.array([0.1, 0.2, 0.8, 4.0, 0.5])
     skewstud = SkewStudent()
     with pytest.warns(InitialValueWarning):
         garch.simulate(parameters, 1000, skewstud.simulate([4.0, 0.5]))