コード例 #1
0
ファイル: test_mean.py プロジェクト: nancy506/arch
    def test_constant_mean_fixed_variance(self):
        rng = RandomState(1234)
        variance = 2 + rng.standard_normal(self.y.shape[0])**2.0
        std = np.sqrt(variance)
        y = pd.Series(std * rng.standard_normal(self.y_series.shape[0]),
                      index=self.y_series.index)

        mod = ConstantMean(y, volatility=FixedVariance(variance))
        res = mod.fit(disp=DISPLAY)
        res.summary()
        assert len(res.params) == 2
        assert "scale" in res.params.index

        mod = ARX(self.y_series,
                  lags=[1, 2, 3],
                  volatility=FixedVariance(variance))
        res = mod.fit(disp=DISPLAY)
        assert len(res.params) == 5
        assert "scale" in res.params.index

        mod = ARX(
            self.y_series,
            lags=[1, 2, 3],
            volatility=FixedVariance(variance, unit_scale=True),
        )
        res = mod.fit(disp=DISPLAY)
        assert len(res.params) == 4
        assert "scale" not in res.params.index
コード例 #2
0
    def test_fixed_variance(self):
        variance = np.arange(1000.0) + 1.0
        fv = FixedVariance(variance)
        fv.start, fv.stop = 0, 1000
        parameters = np.array([2.0])
        resids = self.resids
        sigma2 = np.empty_like(resids)
        backcast = fv.backcast(resids)
        var_bounds = fv.variance_bounds(resids, 2.0)
        fv.compute_variance(parameters, resids, sigma2, backcast, var_bounds)
        sv = fv.starting_values(resids)
        cons = fv.constraints()
        bounds = fv.bounds(resids)
        assert_allclose(sigma2, 2.0 * variance)
        assert_allclose(sv, (resids / np.sqrt(variance)).var())
        assert var_bounds.shape == (resids.shape[0], 2)
        assert fv.num_params == 1
        assert fv.parameter_names() == ['scale']
        assert fv.name == 'Fixed Variance'
        assert_equal(cons[0], np.ones((1, 1)))
        assert_equal(cons[1], np.zeros(1))
        assert_equal(bounds[0][0], sv[0] / 100000.0)

        sigma2 = np.empty(500)
        fv.start = 250
        fv.stop = 750
        fv.compute_variance(parameters, resids[250:750], sigma2, backcast,
                            var_bounds)
        assert_allclose(sigma2, 2.0 * variance[250:750])

        fv = FixedVariance(variance, unit_scale=True)
        fv.start, fv.stop = 0, 1000
        sigma2 = np.empty_like(resids)
        parameters = np.empty(0)
        fv.compute_variance(parameters, resids, sigma2, backcast, var_bounds)
        sv = fv.starting_values(resids)
        cons = fv.constraints()
        bounds = fv.bounds(resids)

        assert_allclose(sigma2, variance)
        assert_allclose(sv, np.empty(0))
        assert fv.num_params == 0
        assert fv.parameter_names() == []
        assert fv.name == 'Fixed Variance (Unit Scale)'
        assert_equal(cons[0], np.empty((0, 0)))
        assert_equal(cons[1], np.empty((0)))
        assert bounds == []
        rng = Normal()
        with pytest.raises(NotImplementedError):
            fv.simulate(parameters, 1000, rng)

        fv = FixedVariance(variance, unit_scale=True)
        fv.start, fv.stop = 123, 731
        sigma2 = np.empty_like(resids)
        parameters = np.empty(0)
        assert fv.start == 123
        assert fv.stop == 731
        with pytest.raises(ValueError):
            fv.compute_variance(parameters, resids, sigma2, backcast,
                                var_bounds)
コード例 #3
0
ファイル: test_mean.py プロジェクト: noisyoscillator/arch
    def test_constant_mean_fixed_variance(self):
        variance = 2 + self.rng.standard_normal(self.y.shape[0]) ** 2.0
        mod = ConstantMean(self.y_series, volatility=FixedVariance(variance))
        res = mod.fit()
        print(res.summary())
        assert len(res.params) == 2
        assert 'scale' in res.params.index

        mod = ARX(self.y_series, lags=[1, 2, 3], volatility=FixedVariance(variance))
        res = mod.fit()
        assert len(res.params) == 5
        assert 'scale' in res.params.index

        mod = ARX(self.y_series, lags=[1, 2, 3],
                  volatility=FixedVariance(variance, unit_scale=True))
        res = mod.fit()
        assert len(res.params) == 4
        assert 'scale' not in res.params.index