def test_studentst(self): dist = StudentsT() v = 4.0 ll1 = dist.loglikelihood(np.array([v]), self.resids, self.sigma2) # Direct calculation of PDF, then log constant = np.exp(gammaln(0.5 * (v + 1)) - gammaln(0.5 * v)) pdf = constant / np.sqrt(np.pi * (v - 2) * self.sigma2) pdf *= (1 + self.resids**2.0 / (self.sigma2 * (v - 2)))**(-(v + 1) / 2) ll2 = np.log(pdf).sum() assert_almost_equal(ll1, ll2) assert_equal(dist.num_params, 1) bounds = dist.bounds(self.resids) assert_equal(len(bounds), 1) a, b = dist.constraints() assert_equal(a.shape, (2, 1)) 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])) with pytest.raises(ValueError): dist.simulate(np.array([1.5]))
def test_studentst(self): dist = StudentsT() v = 4.0 ll1 = dist.loglikelihood(np.array([v]), self.resids, self.sigma2) # Direct calculation of PDF, then log constant = np.exp(gammaln(0.5 * (v + 1)) - gammaln(0.5 * v)) pdf = constant / np.sqrt(np.pi * (v - 2) * self.sigma2) pdf *= (1 + self.resids ** 2.0 / (self.sigma2 * (v - 2))) ** ( -(v + 1) / 2) ll2 = np.log(pdf).sum() assert_almost_equal(ll1, ll2) assert_equal(dist.num_params, 1) bounds = dist.bounds(self.resids) assert_equal(len(bounds), 1) a, b = dist.constraints() assert_equal(a.shape, (2, 1)) 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])) with pytest.raises(ValueError): dist.simulate(np.array([1.5]))
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) 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)
def test_warnings_nonstationary(self): garch = GARCH() parameters = np.array([0.1, 0.2, 0.8, 4.0]) studt = StudentsT() warnings.simplefilter('always', UserWarning) with pytest.warns(InitialValueWarning): garch.simulate(parameters, 1000, studt.simulate([4.0]))
def test_warnings_nonstationary_harch(self): studt = StudentsT() harch = HARCH(lags=[1, 5, 22]) parameters = np.array([0.1, 0.2, 0.4, 0.5]) with pytest.warns(InitialValueWarning): harch.simulate(parameters, 1000, studt.simulate([4.0]))