import os
import timeit
from typing import List

import numpy as np
from numpy.random import RandomState
from numpy.testing import assert_allclose, assert_almost_equal
import pytest
from scipy.special import gamma

import arch.univariate.recursions_python as recpy

CYTHON_COVERAGE = os.environ.get("ARCH_CYTHON_COVERAGE", "0") in ("true", "1", "True")

try:
    import arch.univariate.recursions as rec_cython

    missing_extension = False
except ImportError:
    missing_extension = True

if missing_extension:
    rec = recpy
else:
    rec = rec_cython

try:
    import numba  # noqa

    missing_numba = False
except ImportError:
    missing_numba = True

pytestmark = pytest.mark.filterwarnings("ignore::arch.compat.numba.PerformanceWarning")


class Timer(object):
    def __init__(
        self,
        first,
        first_name,
        second,
        second_name,
        model_name,
        setup,
        repeat=5,
        number=10,
    ) -> None:
        self.first_code = first
        self.second_code = second
        self.setup = setup
        self.first_name = first_name
        self.second_name = second_name
        self.model_name = model_name
        self.repeat = repeat
        self.number = number
        self._run = False
        self.times: List[float] = []
        self._codes = [first, second]
        self.ratio = np.inf

    def display(self):
        if not self._run:
            self.time()
        self.ratio = self.times[0] / self.times[1]
        title = self.model_name + " timing"
        print("\n" + title)
        print("-" * len(title))
        print(self.first_name + ": " + "{:0.3f} ms".format(1000 * self.times[0]))
        print(self.second_name + ": " + "{:0.3f} ms".format(1000 * self.times[1]))
        if self.ratio < 1:
            print(
                "{0} is {1:0.1f}% faster".format(
                    self.first_name, 100 * (1 / self.ratio - 1)
                )
            )
        else:
            print(
                "{0} is {1:0.1f}% faster".format(
                    self.second_name, 100 * (self.ratio - 1)
                )
            )
        print(
            self.first_name
            + "/"
            + self.second_name
            + " Ratio: {:0.3f}\n".format(self.ratio)
        )

    def time(self):
        self.times = []
        for code in self._codes:
            timer = timeit.Timer(code, setup=self.setup)
            self.times.append(min(timer.repeat(self.repeat, self.number)))


class TestRecursions(object):
    @classmethod
    def setup_class(cls):
        cls.nobs = 1000
        cls.rng = RandomState(12345)
        cls.resids = cls.rng.standard_normal(cls.nobs)
        cls.sigma2 = np.zeros_like(cls.resids)
        var = cls.resids.var()
        var_bounds = np.array([var / 1000000.0, var * 1000000.0])
        cls.var_bounds = np.ones((cls.nobs, 2)) * var_bounds
        cls.backcast = 1.0
        cls.timer_setup = """
import numpy as np
import arch.univariate.recursions as rec
import arch.univariate.recursions_python as recpy

nobs = 10000
resids = np.random.standard_normal(nobs)
sigma2 = np.zeros_like(resids)
var = resids.var()
backcast = 1.0
var_bounds = np.array([var / 1000000.0, var * 1000000.0])
var_bounds = np.ones((nobs, 2)) * var_bounds
"""

    def test_garch(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_numba = sigma2.copy()
        recpy.garch_recursion_python(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

        parameters = np.array([0.1, -0.4, 0.3, 0.2])
        recpy.garch_recursion_python(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 0.4, 3, 2])
        recpy.garch_recursion_python(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        mod_fresids = fresids.copy()
        mod_fresids[:1] = np.inf
        recpy.garch_recursion_python(
            parameters,
            mod_fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])
        rec.garch_recursion(
            parameters,
            mod_fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

    def test_harch(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        lags = np.array([1, 5, 22], dtype=np.int32)
        recpy.harch_recursion_python(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        recpy.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        sigma2_numba = sigma2.copy()
        rec.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

        parameters = np.array([-0.1, -0.4, 0.3, 0.2])
        recpy.harch_recursion_python(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 4e8, 3, 2])
        recpy.harch_recursion_python(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 4e8, 3, 2])
        mod_resids = resids.copy()
        mod_resids[:10] = np.inf
        recpy.harch_recursion_python(
            parameters, mod_resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])
        rec.harch_recursion(
            parameters, mod_resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

    def test_arch(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        p = 3

        recpy.arch_recursion_python(
            parameters, resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        recpy.arch_recursion(
            parameters, resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        sigma2_numba = sigma2.copy()
        rec.arch_recursion(
            parameters, resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

        parameters = np.array([-0.1, -0.4, 0.3, 0.2])
        recpy.arch_recursion_python(
            parameters, resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 4e8, 3, 2])
        recpy.arch_recursion_python(
            parameters, resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        mod_resids = resids.copy()
        mod_resids[:10] = np.inf
        recpy.arch_recursion_python(
            parameters, mod_resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])
        rec.arch_recursion(
            parameters, mod_resids, sigma2, p, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

    def test_garch_power_1(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        fresids = np.abs(resids) ** 1.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)

    def test_garch_direct(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        fresids = np.abs(resids) ** 2.0
        sresids = np.sign(resids)

        for t in range(nobs):
            if t == 0:
                sigma2[t] = parameters.dot(
                    np.array([1.0, backcast, 0.5 * backcast, backcast])
                )
            else:
                var = np.array(
                    [
                        1.0,
                        resids[t - 1] ** 2.0,
                        resids[t - 1] ** 2.0 * (resids[t - 1] < 0),
                        sigma2[t - 1],
                    ]
                )
                sigma2[t] = parameters.dot(var)

        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)

    def test_garch_no_q(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            0,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            0,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)

    def test_garch_no_p(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            0,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            0,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)

    def test_garch_no_o(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            0,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            0,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)

    def test_garch_arch(self):
        backcast = self.backcast
        nobs, resids, sigma2 = self.nobs, self.resids, self.sigma2

        parameters = np.array([0.1, 0.4, 0.3, 0.2])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            3,
            0,
            0,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_garch = sigma2.copy()
        rec.arch_recursion(
            parameters, resids, sigma2, 3, nobs, backcast, self.var_bounds
        )

        assert_almost_equal(sigma2_garch, sigma2)

    def test_bounds(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([1e100, 0.4, 0.3, 0.2])
        lags = np.array([1, 5, 22], dtype=np.int32)
        recpy.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        rec.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([-1e100, 0.4, 0.3, 0.2])
        recpy.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        rec.harch_recursion(
            parameters, resids, sigma2, lags, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert_almost_equal(sigma2, self.var_bounds[:, 0])

        parameters = np.array([1e100, 0.4, 0.3, 0.2])
        fresids = resids ** 2.0
        sresids = np.sign(resids)

        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([-1e100, 0.4, 0.3, 0.2])
        recpy.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.garch_recursion(
            parameters,
            fresids,
            sresids,
            sigma2,
            1,
            1,
            1,
            nobs,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert_almost_equal(sigma2, self.var_bounds[:, 0])

        parameters = np.array([1e100, 0.4, 0.3, 0.2])
        recpy.arch_recursion(
            parameters, resids, sigma2, 3, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        rec.arch_recursion(
            parameters, resids, sigma2, 3, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([-1e100, 0.4, 0.3, 0.2])
        recpy.arch_recursion(
            parameters, resids, sigma2, 3, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        rec.arch_recursion(
            parameters, resids, sigma2, 3, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_python, sigma2)
        assert_almost_equal(sigma2, self.var_bounds[:, 0])

    def test_egarch(self):
        nobs = self.nobs
        parameters = np.array([0.0, 0.1, -0.1, 0.95])
        resids, sigma2 = self.resids, self.sigma2
        p = o = q = 1
        backcast = 0.0
        var_bounds = self.var_bounds
        lnsigma2 = np.empty_like(sigma2)
        std_resids = np.empty_like(sigma2)
        abs_std_resids = np.empty_like(sigma2)
        recpy.egarch_recursion(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        sigma2_numba = sigma2.copy()
        recpy.egarch_recursion_python(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        sigma2_python = sigma2.copy()
        rec.egarch_recursion(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

        norm_const = np.sqrt(2 / np.pi)
        for t in range(nobs):
            lnsigma2[t] = parameters[0]
            if t == 0:
                lnsigma2[t] += parameters[3] * backcast
            else:
                stdresid = resids[t - 1] / np.sqrt(sigma2[t - 1])
                lnsigma2[t] += parameters[1] * (np.abs(stdresid) - norm_const)
                lnsigma2[t] += parameters[2] * stdresid
                lnsigma2[t] += parameters[3] * lnsigma2[t - 1]
            sigma2[t] = np.exp(lnsigma2[t])
        assert_almost_equal(sigma2_python, sigma2)

        parameters = np.array([-100.0, 0.1, -0.1, 0.95])
        recpy.egarch_recursion_python(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.0, 0.1, -0.1, 9.5])
        recpy.egarch_recursion_python(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.0, 0.1, -0.1, 0.95])
        mod_resids = resids.copy()
        mod_resids[:1] = np.inf
        recpy.egarch_recursion_python(
            parameters,
            resids,
            sigma2,
            p,
            o,
            q,
            nobs,
            backcast,
            var_bounds,
            lnsigma2,
            std_resids,
            abs_std_resids,
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

    def test_midas_hyperbolic(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast

        parameters = np.array([0.1, 0.8, 0])
        j = np.arange(1, 22 + 1)
        weights = gamma(j + 0.6) / (gamma(j + 1) * gamma(0.6))
        weights = weights / weights.sum()
        recpy.midas_recursion(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        sigma2_numba = sigma2.copy()
        recpy.midas_recursion_python(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        sigma2_python = sigma2.copy()
        rec.midas_recursion(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

        mod_resids = resids.copy()
        mod_resids[:10] = np.inf
        recpy.midas_recursion_python(
            parameters, weights, mod_resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, 10e10, 0])
        j = np.arange(1, 22 + 1)
        weights = gamma(j + 0.6) / (gamma(j + 1) * gamma(0.6))
        weights = weights / weights.sum()
        recpy.midas_recursion_python(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])
        rec.midas_recursion(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

        parameters = np.array([0.1, -0.4, 0])
        recpy.midas_recursion_python(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])
        rec.midas_recursion(
            parameters, weights, resids, sigma2, nobs, backcast, self.var_bounds
        )
        assert np.all(sigma2 >= self.var_bounds[:, 0])
        assert np.all(sigma2 <= 2 * self.var_bounds[:, 1])

    def test_figarch_recursion(self):
        nobs, resids = self.nobs, self.resids
        sigma2, backcast = self.sigma2, self.backcast
        parameters = np.array([1.0, 0.2, 0.4, 0.3])
        fresids = resids ** 2
        p = q = 1
        trunc_lag = 1000
        rec.figarch_recursion(
            parameters,
            fresids,
            sigma2,
            p,
            q,
            nobs,
            trunc_lag,
            backcast,
            self.var_bounds,
        )
        lam = rec.figarch_weights(parameters[1:], p, q, trunc_lag=trunc_lag)
        lam_rev = lam[::-1]
        omega_tilde = parameters[0] / (1 - parameters[-1])
        sigma2_direct = np.empty_like(sigma2)
        for t in range(nobs):
            backcasts = trunc_lag - t
            sigma2_direct[t] = omega_tilde
            if backcasts:
                sigma2_direct[t] += backcast * lam_rev[:backcasts].sum()
            if t:
                sigma2_direct[t] += np.sum(lam_rev[-t:] * fresids[max(0, t - 1000) : t])
        assert_almost_equal(sigma2_direct, sigma2)

        recpy.figarch_recursion(
            parameters,
            fresids,
            sigma2,
            p,
            q,
            nobs,
            trunc_lag,
            backcast,
            self.var_bounds,
        )
        sigma2_numba = sigma2.copy()
        recpy.figarch_recursion_python(
            parameters,
            fresids,
            sigma2,
            p,
            q,
            nobs,
            trunc_lag,
            backcast,
            self.var_bounds,
        )
        sigma2_python = sigma2.copy()
        rec.figarch_recursion(
            parameters,
            fresids,
            sigma2,
            p,
            q,
            nobs,
            trunc_lag,
            backcast,
            self.var_bounds,
        )
        assert_almost_equal(sigma2_numba, sigma2)
        assert_almost_equal(sigma2_python, sigma2)

    def test_figarch_weights(self):
        parameters = np.array([1.0, 0.4])
        lam = rec.figarch_weights(parameters[1:], 0, 0, trunc_lag=1000)
        lam_direct = np.empty_like(lam)
        lam_direct[0] = parameters[-1]
        for i in range(1, 1000):
            lam_direct[i] = (i - parameters[-1]) / (i + 1) * lam_direct[i - 1]
        assert_almost_equal(lam, lam_direct)

    @pytest.mark.skipif(
        missing_numba or missing_extension, reason="numba not installed"
    )
    def test_garch_performance(self):
        garch_setup = """
parameters = np.array([.1, .4, .3, .2])
fresids = resids ** 2.0
sresids = np.sign(resids)
        """

        garch_first = """
recpy.garch_recursion(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs,
backcast, var_bounds)
        """
        garch_second = """
rec.garch_recursion(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs, backcast,
var_bounds)
        """
        timer = Timer(
            garch_first,
            "Numba",
            garch_second,
            "Cython",
            "GARCH",
            self.timer_setup + garch_setup,
        )
        timer.display()
        assert timer.ratio < 10.0
        if not (missing_numba or CYTHON_COVERAGE):
            assert 0.1 < timer.ratio

    @pytest.mark.skipif(
        missing_numba or missing_extension, reason="numba not installed"
    )
    def test_harch_performance(self):
        harch_setup = """
parameters = np.array([.1, .4, .3, .2])
lags = np.array([1, 5, 22], dtype=np.int32)
        """

        harch_first = """
recpy.harch_recursion(parameters, resids, sigma2, lags, nobs, backcast,
var_bounds)
        """

        harch_second = """
rec.harch_recursion(parameters, resids, sigma2, lags, nobs, backcast, var_bounds)
        """

        timer = Timer(
            harch_first,
            "Numba",
            harch_second,
            "Cython",
            "HARCH",
            self.timer_setup + harch_setup,
        )
        timer.display()
        assert timer.ratio < 10.0
        if not (missing_numba or CYTHON_COVERAGE):
            assert 0.1 < timer.ratio

    @pytest.mark.skipif(
        missing_numba or missing_extension, reason="numba not installed"
    )
    def test_egarch_performance(self):
        egarch_setup = """
parameters = np.array([0.0, 0.1, -0.1, 0.95])
p = o = q = 1
backcast = 0.0
lnsigma2 = np.empty_like(sigma2)
std_resids = np.empty_like(sigma2)
abs_std_resids = np.empty_like(sigma2)
        """

        egarch_first = """
recpy.egarch_recursion(parameters, resids, sigma2, p, o, q, nobs, backcast,
var_bounds, lnsigma2, std_resids, abs_std_resids)
"""

        egarch_second = """
rec.egarch_recursion(parameters, resids, sigma2, p, o, q, nobs, backcast,
var_bounds, lnsigma2, std_resids, abs_std_resids)
"""
        timer = Timer(
            egarch_first,
            "Numba",
            egarch_second,
            "Cython",
            "EGARCH",
            self.timer_setup + egarch_setup,
        )
        timer.display()
        assert timer.ratio < 10.0
        if not (missing_numba or CYTHON_COVERAGE):
            assert 0.1 < timer.ratio

    @pytest.mark.skipif(
        missing_numba or missing_extension, reason="numba not installed"
    )
    def test_midas_performance(self):
        midas_setup = """
from scipy.special import gamma
parameters = np.array([.1, 0.8, 0])
j = np.arange(1,22+1)
weights = gamma(j+0.6) / (gamma(j+1) * gamma(0.6))
weights = weights / weights.sum()
"""

        midas_first = """
recpy.midas_recursion(parameters, weights, resids, sigma2, nobs, backcast, var_bounds)
                """
        midas_second = """
rec.midas_recursion(parameters, weights, resids, sigma2, nobs, backcast, var_bounds)
"""
        timer = Timer(
            midas_first,
            "Numba",
            midas_second,
            "Cython",
            "MIDAS",
            self.timer_setup + midas_setup,
        )
        timer.display()
        assert timer.ratio < 10.0
        if not (missing_numba or CYTHON_COVERAGE):
            assert 0.1 < timer.ratio

    @pytest.mark.skipif(
        missing_numba or missing_extension, reason="numba not installed"
    )
    def test_figarch_performance(self):
        midas_setup = """
p = q = 1
trunc_lag = 1000
parameters = np.array([1.0, 0.2, 0.2, 0.04])
fresids = resids ** 2.0
"""

        midas_first = """
recpy.figarch_recursion(parameters, fresids, sigma2, p, q, nobs, trunc_lag, backcast, var_bounds)
                """
        midas_second = """
rec.figarch_recursion(parameters, fresids, sigma2, p, q, nobs, trunc_lag, backcast, var_bounds)
"""
        timer = Timer(
            midas_first,
            "Numba",
            midas_second,
            "Cython",
            "FIGARCH",
            self.timer_setup + midas_setup,
        )
        timer.display()
        assert timer.ratio < 10.0
        if not (missing_numba or CYTHON_COVERAGE):
            assert 0.1 < timer.ratio

    def test_garch_aparch_equiv(self):
        parameters = np.array([0.1, 0.1, 0.8])
        fresids = self.resids ** 2
        sresids = np.sign(self.resids)
        sigma2 = np.empty(1000)
        p = q = 1
        o = 0
        recpy.garch_recursion_python(
            parameters,
            fresids,
            sresids,
            sigma2,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        sigma2_garch = sigma2.copy()

        parameters = np.array([0.1, 0.1, 0.8, 2])
        sigma2[:] = np.nan
        sigma2_delta = np.empty_like(sigma2)
        recpy.aparch_recursion_python(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert_allclose(sigma2_garch, sigma2, atol=1e-6)

        sigma2[:] = np.nan
        recpy.aparch_recursion(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert_allclose(sigma2_garch, sigma2, atol=1e-6)

        sigma2[:] = np.nan
        rec.aparch_recursion(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert_allclose(sigma2_garch, sigma2, atol=1e-6)

    def test_asym_aparch_smoke(self):
        sigma2 = np.empty(1000)
        p = o = q = 1
        parameters = np.array([0.1, 0.1, 0.1, 0.8, 1.3])
        sigma2[:] = np.nan
        sigma2_delta = np.empty_like(sigma2)
        recpy.aparch_recursion_python(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert np.all(np.isfinite(sigma2))
        sigma2_py = sigma2.copy()
        sigma2[:] = np.nan
        recpy.aparch_recursion(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert np.all(np.isfinite(sigma2))
        assert_allclose(sigma2_py, sigma2)

        sigma2[:] = np.nan
        rec.aparch_recursion(
            parameters,
            self.resids,
            np.abs(self.resids),
            sigma2,
            sigma2_delta,
            p,
            o,
            q,
            self.nobs,
            self.backcast,
            self.var_bounds,
        )
        assert np.all(np.isfinite(sigma2))
        assert_allclose(sigma2_py, sigma2)


def test_bounds_check():
    var_bounds = np.array([0.1, 10])
    assert_almost_equal(recpy.bounds_check_python(-1.0, var_bounds), 0.1)
    assert_almost_equal(
        recpy.bounds_check_python(20.0, var_bounds), 10 + np.log(20.0 / 10.0)
    )
    assert_almost_equal(recpy.bounds_check_python(np.inf, var_bounds), 1010.0)

[((674, 748), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', (['"""ignore::arch.compat.numba.PerformanceWarning"""'], {}), "('ignore::arch.compat.numba.PerformanceWarning')\n", (700, 748), False, 'import pytest\n'), ((284, 327), 'os.environ.get', 'os.environ.get', (['"""ARCH_CYTHON_COVERAGE"""', '"""0"""'], {}), "('ARCH_CYTHON_COVERAGE', '0')\n", (298, 327), False, 'import os\n'), ((26657, 26746), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(missing_numba or missing_extension)'], {'reason': '"""numba not installed"""'}), "(missing_numba or missing_extension, reason=\n    'numba not installed')\n", (26675, 26746), False, 'import pytest\n'), ((27536, 27625), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(missing_numba or missing_extension)'], {'reason': '"""numba not installed"""'}), "(missing_numba or missing_extension, reason=\n    'numba not installed')\n", (27554, 27625), False, 'import pytest\n'), ((28385, 28474), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(missing_numba or missing_extension)'], {'reason': '"""numba not installed"""'}), "(missing_numba or missing_extension, reason=\n    'numba not installed')\n", (28403, 28474), False, 'import pytest\n'), ((29407, 29496), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(missing_numba or missing_extension)'], {'reason': '"""numba not installed"""'}), "(missing_numba or missing_extension, reason=\n    'numba not installed')\n", (29425, 29496), False, 'import pytest\n'), ((30343, 30432), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(missing_numba or missing_extension)'], {'reason': '"""numba not installed"""'}), "(missing_numba or missing_extension, reason=\n    'numba not installed')\n", (30361, 30432), False, 'import pytest\n'), ((34434, 34453), 'numpy.array', 'np.array', (['[0.1, 10]'], {}), '([0.1, 10])\n', (34442, 34453), True, 'import numpy as np\n'), ((2600, 2618), 'numpy.random.RandomState', 'RandomState', (['(12345)'], {}), '(12345)\n', (2611, 2618), False, 'from numpy.random import RandomState\n'), ((2695, 2720), 'numpy.zeros_like', 'np.zeros_like', (['cls.resids'], {}), '(cls.resids)\n', (2708, 2720), True, 'import numpy as np\n'), ((2773, 2817), 'numpy.array', 'np.array', (['[var / 1000000.0, var * 1000000.0]'], {}), '([var / 1000000.0, var * 1000000.0])\n', (2781, 2817), True, 'import numpy as np\n'), ((3422, 3452), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (3430, 3452), True, 'import numpy as np\n'), ((3503, 3518), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (3510, 3518), True, 'import numpy as np\n'), ((3528, 3633), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs,\n    backcast, self.var_bounds)\n', (3549, 3633), True, 'import arch.univariate.recursions_python as recpy\n'), ((3806, 3918), 'arch.univariate.recursions_python.garch_recursion_python', 'recpy.garch_recursion_python', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1,\n    nobs, backcast, self.var_bounds)\n', (3834, 3918), True, 'import arch.univariate.recursions_python as recpy\n'), ((4331, 4372), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (4350, 4372), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((4381, 4423), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (4400, 4423), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((4446, 4477), 'numpy.array', 'np.array', (['[0.1, -0.4, 0.3, 0.2]'], {}), '([0.1, -0.4, 0.3, 0.2])\n', (4454, 4477), True, 'import numpy as np\n'), ((4486, 4598), 'arch.univariate.recursions_python.garch_recursion_python', 'recpy.garch_recursion_python', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1,\n    nobs, backcast, self.var_bounds)\n', (4514, 4598), True, 'import arch.univariate.recursions_python as recpy\n'), ((4741, 4782), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (4747, 4782), True, 'import numpy as np\n'), ((4796, 4841), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (4802, 4841), True, 'import numpy as np\n'), ((4862, 4888), 'numpy.array', 'np.array', (['[0.1, 0.4, 3, 2]'], {}), '([0.1, 0.4, 3, 2])\n', (4870, 4888), True, 'import numpy as np\n'), ((4897, 5009), 'arch.univariate.recursions_python.garch_recursion_python', 'recpy.garch_recursion_python', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1,\n    nobs, backcast, self.var_bounds)\n', (4925, 5009), True, 'import arch.univariate.recursions_python as recpy\n'), ((5152, 5193), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (5158, 5193), True, 'import numpy as np\n'), ((5207, 5252), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (5213, 5252), True, 'import numpy as np\n'), ((5273, 5303), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (5281, 5303), True, 'import numpy as np\n'), ((5382, 5498), 'arch.univariate.recursions_python.garch_recursion_python', 'recpy.garch_recursion_python', (['parameters', 'mod_fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, mod_fresids, sresids, sigma2, 1, 1,\n    1, nobs, backcast, self.var_bounds)\n', (5410, 5498), True, 'import arch.univariate.recursions_python as recpy\n'), ((5641, 5682), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (5647, 5682), True, 'import numpy as np\n'), ((5696, 5741), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (5702, 5741), True, 'import numpy as np\n'), ((5998, 6039), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (6004, 6039), True, 'import numpy as np\n'), ((6053, 6098), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (6059, 6098), True, 'import numpy as np\n'), ((6246, 6276), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (6254, 6276), True, 'import numpy as np\n'), ((6292, 6328), 'numpy.array', 'np.array', (['[1, 5, 22]'], {'dtype': 'np.int32'}), '([1, 5, 22], dtype=np.int32)\n', (6300, 6328), True, 'import numpy as np\n'), ((6337, 6436), 'arch.univariate.recursions_python.harch_recursion_python', 'recpy.harch_recursion_python', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs,\n    backcast, self.var_bounds)\n', (6365, 6436), True, 'import arch.univariate.recursions_python as recpy\n'), ((6501, 6593), 'arch.univariate.recursions_python.harch_recursion', 'recpy.harch_recursion', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs, backcast,\n    self.var_bounds)\n', (6522, 6593), True, 'import arch.univariate.recursions_python as recpy\n'), ((6774, 6815), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (6793, 6815), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((6824, 6866), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (6843, 6866), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((6889, 6921), 'numpy.array', 'np.array', (['[-0.1, -0.4, 0.3, 0.2]'], {}), '([-0.1, -0.4, 0.3, 0.2])\n', (6897, 6921), True, 'import numpy as np\n'), ((6930, 7029), 'arch.univariate.recursions_python.harch_recursion_python', 'recpy.harch_recursion_python', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs,\n    backcast, self.var_bounds)\n', (6958, 7029), True, 'import arch.univariate.recursions_python as recpy\n'), ((7063, 7104), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (7069, 7104), True, 'import numpy as np\n'), ((7118, 7163), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (7124, 7163), True, 'import numpy as np\n'), ((7184, 7218), 'numpy.array', 'np.array', (['[0.1, 400000000.0, 3, 2]'], {}), '([0.1, 400000000.0, 3, 2])\n', (7192, 7218), True, 'import numpy as np\n'), ((7219, 7318), 'arch.univariate.recursions_python.harch_recursion_python', 'recpy.harch_recursion_python', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs,\n    backcast, self.var_bounds)\n', (7247, 7318), True, 'import arch.univariate.recursions_python as recpy\n'), ((7352, 7393), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (7358, 7393), True, 'import numpy as np\n'), ((7407, 7452), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (7413, 7452), True, 'import numpy as np\n'), ((7473, 7507), 'numpy.array', 'np.array', (['[0.1, 400000000.0, 3, 2]'], {}), '([0.1, 400000000.0, 3, 2])\n', (7481, 7507), True, 'import numpy as np\n'), ((7576, 7679), 'arch.univariate.recursions_python.harch_recursion_python', 'recpy.harch_recursion_python', (['parameters', 'mod_resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, mod_resids, sigma2, lags, nobs,\n    backcast, self.var_bounds)\n', (7604, 7679), True, 'import arch.univariate.recursions_python as recpy\n'), ((7713, 7754), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (7719, 7754), True, 'import numpy as np\n'), ((7768, 7813), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (7774, 7813), True, 'import numpy as np\n'), ((7948, 7989), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (7954, 7989), True, 'import numpy as np\n'), ((8003, 8048), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (8009, 8048), True, 'import numpy as np\n'), ((8195, 8225), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (8203, 8225), True, 'import numpy as np\n'), ((8249, 8344), 'arch.univariate.recursions_python.arch_recursion_python', 'recpy.arch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, p, nobs, backcast,\n    self.var_bounds)\n', (8276, 8344), True, 'import arch.univariate.recursions_python as recpy\n'), ((8409, 8498), 'arch.univariate.recursions_python.arch_recursion', 'recpy.arch_recursion', (['parameters', 'resids', 'sigma2', 'p', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, p, nobs, backcast, self.\n    var_bounds)\n', (8429, 8498), True, 'import arch.univariate.recursions_python as recpy\n'), ((8674, 8715), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (8693, 8715), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((8724, 8766), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (8743, 8766), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((8789, 8821), 'numpy.array', 'np.array', (['[-0.1, -0.4, 0.3, 0.2]'], {}), '([-0.1, -0.4, 0.3, 0.2])\n', (8797, 8821), True, 'import numpy as np\n'), ((8830, 8925), 'arch.univariate.recursions_python.arch_recursion_python', 'recpy.arch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, p, nobs, backcast,\n    self.var_bounds)\n', (8857, 8925), True, 'import arch.univariate.recursions_python as recpy\n'), ((8959, 9000), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (8965, 9000), True, 'import numpy as np\n'), ((9014, 9059), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (9020, 9059), True, 'import numpy as np\n'), ((9080, 9114), 'numpy.array', 'np.array', (['[0.1, 400000000.0, 3, 2]'], {}), '([0.1, 400000000.0, 3, 2])\n', (9088, 9114), True, 'import numpy as np\n'), ((9115, 9210), 'arch.univariate.recursions_python.arch_recursion_python', 'recpy.arch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, p, nobs, backcast,\n    self.var_bounds)\n', (9142, 9210), True, 'import arch.univariate.recursions_python as recpy\n'), ((9244, 9285), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (9250, 9285), True, 'import numpy as np\n'), ((9299, 9344), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (9305, 9344), True, 'import numpy as np\n'), ((9420, 9519), 'arch.univariate.recursions_python.arch_recursion_python', 'recpy.arch_recursion_python', (['parameters', 'mod_resids', 'sigma2', 'p', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, mod_resids, sigma2, p, nobs,\n    backcast, self.var_bounds)\n', (9447, 9519), True, 'import arch.univariate.recursions_python as recpy\n'), ((9553, 9594), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (9559, 9594), True, 'import numpy as np\n'), ((9608, 9653), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (9614, 9653), True, 'import numpy as np\n'), ((9784, 9825), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (9790, 9825), True, 'import numpy as np\n'), ((9839, 9884), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (9845, 9884), True, 'import numpy as np\n'), ((10040, 10070), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (10048, 10070), True, 'import numpy as np\n'), ((10129, 10144), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (10136, 10144), True, 'import numpy as np\n'), ((10154, 10259), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs,\n    backcast, self.var_bounds)\n', (10175, 10259), True, 'import arch.univariate.recursions_python as recpy\n'), ((10672, 10714), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (10691, 10714), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((10871, 10901), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (10879, 10901), True, 'import numpy as np\n'), ((10960, 10975), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (10967, 10975), True, 'import numpy as np\n'), ((11792, 11834), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (11811, 11834), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((11989, 12014), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3]'], {}), '([0.1, 0.4, 0.3])\n', (11997, 12014), True, 'import numpy as np\n'), ((12065, 12080), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (12072, 12080), True, 'import numpy as np\n'), ((12090, 12195), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(0)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 0, nobs,\n    backcast, self.var_bounds)\n', (12111, 12195), True, 'import arch.univariate.recursions_python as recpy\n'), ((12608, 12650), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (12627, 12650), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((12805, 12830), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3]'], {}), '([0.1, 0.4, 0.3])\n', (12813, 12830), True, 'import numpy as np\n'), ((12881, 12896), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (12888, 12896), True, 'import numpy as np\n'), ((12906, 13011), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(0)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 0, 1, 1, nobs,\n    backcast, self.var_bounds)\n', (12927, 13011), True, 'import arch.univariate.recursions_python as recpy\n'), ((13424, 13466), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (13443, 13466), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((13621, 13651), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (13629, 13651), True, 'import numpy as np\n'), ((13702, 13717), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (13709, 13717), True, 'import numpy as np\n'), ((13727, 13832), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(0)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 0, 1, nobs,\n    backcast, self.var_bounds)\n', (13748, 13832), True, 'import arch.univariate.recursions_python as recpy\n'), ((14245, 14287), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (14264, 14287), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((14442, 14472), 'numpy.array', 'np.array', (['[0.1, 0.4, 0.3, 0.2]'], {}), '([0.1, 0.4, 0.3, 0.2])\n', (14450, 14472), True, 'import numpy as np\n'), ((14523, 14538), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (14530, 14538), True, 'import numpy as np\n'), ((14938, 14979), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_garch', 'sigma2'], {}), '(sigma2_garch, sigma2)\n', (14957, 14979), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((15130, 15163), 'numpy.array', 'np.array', (['[1e+100, 0.4, 0.3, 0.2]'], {}), '([1e+100, 0.4, 0.3, 0.2])\n', (15138, 15163), True, 'import numpy as np\n'), ((15178, 15214), 'numpy.array', 'np.array', (['[1, 5, 22]'], {'dtype': 'np.int32'}), '([1, 5, 22], dtype=np.int32)\n', (15186, 15214), True, 'import numpy as np\n'), ((15223, 15315), 'arch.univariate.recursions_python.harch_recursion', 'recpy.harch_recursion', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs, backcast,\n    self.var_bounds)\n', (15244, 15315), True, 'import arch.univariate.recursions_python as recpy\n'), ((15497, 15539), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (15516, 15539), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((15555, 15596), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (15561, 15596), True, 'import numpy as np\n'), ((15610, 15655), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (15616, 15655), True, 'import numpy as np\n'), ((15676, 15710), 'numpy.array', 'np.array', (['[-1e+100, 0.4, 0.3, 0.2]'], {}), '([-1e+100, 0.4, 0.3, 0.2])\n', (15684, 15710), True, 'import numpy as np\n'), ((15718, 15810), 'arch.univariate.recursions_python.harch_recursion', 'recpy.harch_recursion', (['parameters', 'resids', 'sigma2', 'lags', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, lags, nobs, backcast,\n    self.var_bounds)\n', (15739, 15810), True, 'import arch.univariate.recursions_python as recpy\n'), ((15992, 16034), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (16011, 16034), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((16043, 16095), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2', 'self.var_bounds[:, (0)]'], {}), '(sigma2, self.var_bounds[:, (0)])\n', (16062, 16095), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((16116, 16149), 'numpy.array', 'np.array', (['[1e+100, 0.4, 0.3, 0.2]'], {}), '([1e+100, 0.4, 0.3, 0.2])\n', (16124, 16149), True, 'import numpy as np\n'), ((16199, 16214), 'numpy.sign', 'np.sign', (['resids'], {}), '(resids)\n', (16206, 16214), True, 'import numpy as np\n'), ((16224, 16329), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs,\n    backcast, self.var_bounds)\n', (16245, 16329), True, 'import arch.univariate.recursions_python as recpy\n'), ((16742, 16784), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (16761, 16784), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((16800, 16841), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (16806, 16841), True, 'import numpy as np\n'), ((16855, 16900), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (16861, 16900), True, 'import numpy as np\n'), ((16921, 16955), 'numpy.array', 'np.array', (['[-1e+100, 0.4, 0.3, 0.2]'], {}), '([-1e+100, 0.4, 0.3, 0.2])\n', (16929, 16955), True, 'import numpy as np\n'), ((16963, 17068), 'arch.univariate.recursions_python.garch_recursion', 'recpy.garch_recursion', (['parameters', 'fresids', 'sresids', 'sigma2', '(1)', '(1)', '(1)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, 1, 1, 1, nobs,\n    backcast, self.var_bounds)\n', (16984, 17068), True, 'import arch.univariate.recursions_python as recpy\n'), ((17481, 17523), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (17500, 17523), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((17532, 17584), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2', 'self.var_bounds[:, (0)]'], {}), '(sigma2, self.var_bounds[:, (0)])\n', (17551, 17584), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((17605, 17638), 'numpy.array', 'np.array', (['[1e+100, 0.4, 0.3, 0.2]'], {}), '([1e+100, 0.4, 0.3, 0.2])\n', (17613, 17638), True, 'import numpy as np\n'), ((17646, 17735), 'arch.univariate.recursions_python.arch_recursion', 'recpy.arch_recursion', (['parameters', 'resids', 'sigma2', '(3)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, 3, nobs, backcast, self.\n    var_bounds)\n', (17666, 17735), True, 'import arch.univariate.recursions_python as recpy\n'), ((17912, 17954), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (17931, 17954), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((17970, 18011), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (17976, 18011), True, 'import numpy as np\n'), ((18025, 18070), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (18031, 18070), True, 'import numpy as np\n'), ((18091, 18125), 'numpy.array', 'np.array', (['[-1e+100, 0.4, 0.3, 0.2]'], {}), '([-1e+100, 0.4, 0.3, 0.2])\n', (18099, 18125), True, 'import numpy as np\n'), ((18133, 18222), 'arch.univariate.recursions_python.arch_recursion', 'recpy.arch_recursion', (['parameters', 'resids', 'sigma2', '(3)', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, resids, sigma2, 3, nobs, backcast, self.\n    var_bounds)\n', (18153, 18222), True, 'import arch.univariate.recursions_python as recpy\n'), ((18399, 18441), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (18418, 18441), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((18450, 18502), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2', 'self.var_bounds[:, (0)]'], {}), '(sigma2, self.var_bounds[:, (0)])\n', (18469, 18502), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((18575, 18607), 'numpy.array', 'np.array', (['[0.0, 0.1, -0.1, 0.95]'], {}), '([0.0, 0.1, -0.1, 0.95])\n', (18583, 18607), True, 'import numpy as np\n'), ((18759, 18780), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (18772, 18780), True, 'import numpy as np\n'), ((18802, 18823), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (18815, 18823), True, 'import numpy as np\n'), ((18849, 18870), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (18862, 18870), True, 'import numpy as np\n'), ((18879, 19008), 'arch.univariate.recursions_python.egarch_recursion', 'recpy.egarch_recursion', (['parameters', 'resids', 'sigma2', 'p', 'o', 'q', 'nobs', 'backcast', 'var_bounds', 'lnsigma2', 'std_resids', 'abs_std_resids'], {}), '(parameters, resids, sigma2, p, o, q, nobs, backcast,\n    var_bounds, lnsigma2, std_resids, abs_std_resids)\n', (18901, 19008), True, 'import arch.univariate.recursions_python as recpy\n'), ((19205, 19341), 'arch.univariate.recursions_python.egarch_recursion_python', 'recpy.egarch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'o', 'q', 'nobs', 'backcast', 'var_bounds', 'lnsigma2', 'std_resids', 'abs_std_resids'], {}), '(parameters, resids, sigma2, p, o, q, nobs,\n    backcast, var_bounds, lnsigma2, std_resids, abs_std_resids)\n', (19234, 19341), True, 'import arch.univariate.recursions_python as recpy\n'), ((19826, 19867), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (19845, 19867), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((19876, 19918), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (19895, 19918), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((19941, 19959), 'numpy.sqrt', 'np.sqrt', (['(2 / np.pi)'], {}), '(2 / np.pi)\n', (19948, 19959), True, 'import numpy as np\n'), ((20443, 20485), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (20462, 20485), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((20508, 20543), 'numpy.array', 'np.array', (['[-100.0, 0.1, -0.1, 0.95]'], {}), '([-100.0, 0.1, -0.1, 0.95])\n', (20516, 20543), True, 'import numpy as np\n'), ((20552, 20688), 'arch.univariate.recursions_python.egarch_recursion_python', 'recpy.egarch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'o', 'q', 'nobs', 'backcast', 'var_bounds', 'lnsigma2', 'std_resids', 'abs_std_resids'], {}), '(parameters, resids, sigma2, p, o, q, nobs,\n    backcast, var_bounds, lnsigma2, std_resids, abs_std_resids)\n', (20581, 20688), True, 'import arch.univariate.recursions_python as recpy\n'), ((20855, 20896), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (20861, 20896), True, 'import numpy as np\n'), ((20910, 20955), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (20916, 20955), True, 'import numpy as np\n'), ((20976, 21007), 'numpy.array', 'np.array', (['[0.0, 0.1, -0.1, 9.5]'], {}), '([0.0, 0.1, -0.1, 9.5])\n', (20984, 21007), True, 'import numpy as np\n'), ((21016, 21152), 'arch.univariate.recursions_python.egarch_recursion_python', 'recpy.egarch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'o', 'q', 'nobs', 'backcast', 'var_bounds', 'lnsigma2', 'std_resids', 'abs_std_resids'], {}), '(parameters, resids, sigma2, p, o, q, nobs,\n    backcast, var_bounds, lnsigma2, std_resids, abs_std_resids)\n', (21045, 21152), True, 'import arch.univariate.recursions_python as recpy\n'), ((21319, 21360), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (21325, 21360), True, 'import numpy as np\n'), ((21374, 21419), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (21380, 21419), True, 'import numpy as np\n'), ((21440, 21472), 'numpy.array', 'np.array', (['[0.0, 0.1, -0.1, 0.95]'], {}), '([0.0, 0.1, -0.1, 0.95])\n', (21448, 21472), True, 'import numpy as np\n'), ((21548, 21684), 'arch.univariate.recursions_python.egarch_recursion_python', 'recpy.egarch_recursion_python', (['parameters', 'resids', 'sigma2', 'p', 'o', 'q', 'nobs', 'backcast', 'var_bounds', 'lnsigma2', 'std_resids', 'abs_std_resids'], {}), '(parameters, resids, sigma2, p, o, q, nobs,\n    backcast, var_bounds, lnsigma2, std_resids, abs_std_resids)\n', (21577, 21684), True, 'import arch.univariate.recursions_python as recpy\n'), ((21851, 21892), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (21857, 21892), True, 'import numpy as np\n'), ((21906, 21951), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (21912, 21951), True, 'import numpy as np\n'), ((22110, 22133), 'numpy.array', 'np.array', (['[0.1, 0.8, 0]'], {}), '([0.1, 0.8, 0])\n', (22118, 22133), True, 'import numpy as np\n'), ((22146, 22166), 'numpy.arange', 'np.arange', (['(1)', '(22 + 1)'], {}), '(1, 22 + 1)\n', (22155, 22166), True, 'import numpy as np\n'), ((22280, 22375), 'arch.univariate.recursions_python.midas_recursion', 'recpy.midas_recursion', (['parameters', 'weights', 'resids', 'sigma2', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, weights, resids, sigma2, nobs, backcast,\n    self.var_bounds)\n', (22301, 22375), True, 'import arch.univariate.recursions_python as recpy\n'), ((22439, 22541), 'arch.univariate.recursions_python.midas_recursion_python', 'recpy.midas_recursion_python', (['parameters', 'weights', 'resids', 'sigma2', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, weights, resids, sigma2, nobs,\n    backcast, self.var_bounds)\n', (22467, 22541), True, 'import arch.univariate.recursions_python as recpy\n'), ((22726, 22767), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (22745, 22767), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((22776, 22818), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (22795, 22818), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((22896, 23002), 'arch.univariate.recursions_python.midas_recursion_python', 'recpy.midas_recursion_python', (['parameters', 'weights', 'mod_resids', 'sigma2', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, weights, mod_resids, sigma2, nobs,\n    backcast, self.var_bounds)\n', (22924, 23002), True, 'import arch.univariate.recursions_python as recpy\n'), ((23036, 23077), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (23042, 23077), True, 'import numpy as np\n'), ((23091, 23136), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (23097, 23136), True, 'import numpy as np\n'), ((23157, 23191), 'numpy.array', 'np.array', (['[0.1, 100000000000.0, 0]'], {}), '([0.1, 100000000000.0, 0])\n', (23165, 23191), True, 'import numpy as np\n'), ((23195, 23215), 'numpy.arange', 'np.arange', (['(1)', '(22 + 1)'], {}), '(1, 22 + 1)\n', (23204, 23215), True, 'import numpy as np\n'), ((23329, 23431), 'arch.univariate.recursions_python.midas_recursion_python', 'recpy.midas_recursion_python', (['parameters', 'weights', 'resids', 'sigma2', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, weights, resids, sigma2, nobs,\n    backcast, self.var_bounds)\n', (23357, 23431), True, 'import arch.univariate.recursions_python as recpy\n'), ((23465, 23506), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (23471, 23506), True, 'import numpy as np\n'), ((23520, 23565), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (23526, 23565), True, 'import numpy as np\n'), ((23699, 23740), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (23705, 23740), True, 'import numpy as np\n'), ((23754, 23799), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (23760, 23799), True, 'import numpy as np\n'), ((23820, 23844), 'numpy.array', 'np.array', (['[0.1, -0.4, 0]'], {}), '([0.1, -0.4, 0])\n', (23828, 23844), True, 'import numpy as np\n'), ((23853, 23955), 'arch.univariate.recursions_python.midas_recursion_python', 'recpy.midas_recursion_python', (['parameters', 'weights', 'resids', 'sigma2', 'nobs', 'backcast', 'self.var_bounds'], {}), '(parameters, weights, resids, sigma2, nobs,\n    backcast, self.var_bounds)\n', (23881, 23955), True, 'import arch.univariate.recursions_python as recpy\n'), ((23989, 24030), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (23995, 24030), True, 'import numpy as np\n'), ((24044, 24089), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (24050, 24089), True, 'import numpy as np\n'), ((24223, 24264), 'numpy.all', 'np.all', (['(sigma2 >= self.var_bounds[:, (0)])'], {}), '(sigma2 >= self.var_bounds[:, (0)])\n', (24229, 24264), True, 'import numpy as np\n'), ((24278, 24323), 'numpy.all', 'np.all', (['(sigma2 <= 2 * self.var_bounds[:, (1)])'], {}), '(sigma2 <= 2 * self.var_bounds[:, (1)])\n', (24284, 24323), True, 'import numpy as np\n'), ((24482, 24512), 'numpy.array', 'np.array', (['[1.0, 0.2, 0.4, 0.3]'], {}), '([1.0, 0.2, 0.4, 0.3])\n', (24490, 24512), True, 'import numpy as np\n'), ((25002, 25023), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (25015, 25023), True, 'import numpy as np\n'), ((25349, 25391), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_direct', 'sigma2'], {}), '(sigma2_direct, sigma2)\n', (25368, 25391), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((25401, 25507), 'arch.univariate.recursions_python.figarch_recursion', 'recpy.figarch_recursion', (['parameters', 'fresids', 'sigma2', 'p', 'q', 'nobs', 'trunc_lag', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sigma2, p, q, nobs, trunc_lag,\n    backcast, self.var_bounds)\n', (25424, 25507), True, 'import arch.univariate.recursions_python as recpy\n'), ((25668, 25781), 'arch.univariate.recursions_python.figarch_recursion_python', 'recpy.figarch_recursion_python', (['parameters', 'fresids', 'sigma2', 'p', 'q', 'nobs', 'trunc_lag', 'backcast', 'self.var_bounds'], {}), '(parameters, fresids, sigma2, p, q, nobs,\n    trunc_lag, backcast, self.var_bounds)\n', (25698, 25781), True, 'import arch.univariate.recursions_python as recpy\n'), ((26171, 26212), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_numba', 'sigma2'], {}), '(sigma2_numba, sigma2)\n', (26190, 26212), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((26221, 26263), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['sigma2_python', 'sigma2'], {}), '(sigma2_python, sigma2)\n', (26240, 26263), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((26322, 26342), 'numpy.array', 'np.array', (['[1.0, 0.4]'], {}), '([1.0, 0.4])\n', (26330, 26342), True, 'import numpy as np\n'), ((26436, 26454), 'numpy.empty_like', 'np.empty_like', (['lam'], {}), '(lam)\n', (26449, 26454), True, 'import numpy as np\n'), ((26614, 26650), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['lam', 'lam_direct'], {}), '(lam, lam_direct)\n', (26633, 26650), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((31281, 31306), 'numpy.array', 'np.array', (['[0.1, 0.1, 0.8]'], {}), '([0.1, 0.1, 0.8])\n', (31289, 31306), True, 'import numpy as np\n'), ((31360, 31380), 'numpy.sign', 'np.sign', (['self.resids'], {}), '(self.resids)\n', (31367, 31380), True, 'import numpy as np\n'), ((31398, 31412), 'numpy.empty', 'np.empty', (['(1000)'], {}), '(1000)\n', (31406, 31412), True, 'import numpy as np\n'), ((31453, 31575), 'arch.univariate.recursions_python.garch_recursion_python', 'recpy.garch_recursion_python', (['parameters', 'fresids', 'sresids', 'sigma2', 'p', 'o', 'q', 'self.nobs', 'self.backcast', 'self.var_bounds'], {}), '(parameters, fresids, sresids, sigma2, p, o, q,\n    self.nobs, self.backcast, self.var_bounds)\n', (31481, 31575), True, 'import arch.univariate.recursions_python as recpy\n'), ((31762, 31790), 'numpy.array', 'np.array', (['[0.1, 0.1, 0.8, 2]'], {}), '([0.1, 0.1, 0.8, 2])\n', (31770, 31790), True, 'import numpy as np\n'), ((31841, 31862), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (31854, 31862), True, 'import numpy as np\n'), ((32172, 32221), 'numpy.testing.assert_allclose', 'assert_allclose', (['sigma2_garch', 'sigma2'], {'atol': '(1e-06)'}), '(sigma2_garch, sigma2, atol=1e-06)\n', (32187, 32221), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((32551, 32600), 'numpy.testing.assert_allclose', 'assert_allclose', (['sigma2_garch', 'sigma2'], {'atol': '(1e-06)'}), '(sigma2_garch, sigma2, atol=1e-06)\n', (32566, 32600), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((32928, 32977), 'numpy.testing.assert_allclose', 'assert_allclose', (['sigma2_garch', 'sigma2'], {'atol': '(1e-06)'}), '(sigma2_garch, sigma2, atol=1e-06)\n', (32943, 32977), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((33033, 33047), 'numpy.empty', 'np.empty', (['(1000)'], {}), '(1000)\n', (33041, 33047), True, 'import numpy as np\n'), ((33091, 33126), 'numpy.array', 'np.array', (['[0.1, 0.1, 0.1, 0.8, 1.3]'], {}), '([0.1, 0.1, 0.1, 0.8, 1.3])\n', (33099, 33126), True, 'import numpy as np\n'), ((33177, 33198), 'numpy.empty_like', 'np.empty_like', (['sigma2'], {}), '(sigma2)\n', (33190, 33198), True, 'import numpy as np\n'), ((33949, 33983), 'numpy.testing.assert_allclose', 'assert_allclose', (['sigma2_py', 'sigma2'], {}), '(sigma2_py, sigma2)\n', (33964, 33983), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((34355, 34389), 'numpy.testing.assert_allclose', 'assert_allclose', (['sigma2_py', 'sigma2'], {}), '(sigma2_py, sigma2)\n', (34370, 34389), False, 'from numpy.testing import assert_allclose, assert_almost_equal\n'), ((34478, 34521), 'arch.univariate.recursions_python.bounds_check_python', 'recpy.bounds_check_python', (['(-1.0)', 'var_bounds'], {}), '(-1.0, var_bounds)\n', (34503, 34521), True, 'import arch.univariate.recursions_python as recpy\n'), ((34561, 34604), 'arch.univariate.recursions_python.bounds_check_python', 'recpy.bounds_check_python', (['(20.0)', 'var_bounds'], {}), '(20.0, var_bounds)\n', (34586, 34604), True, 'import arch.univariate.recursions_python as recpy\n'), ((34661, 34706), 'arch.univariate.recursions_python.bounds_check_python', 'recpy.bounds_check_python', (['np.inf', 'var_bounds'], {}), '(np.inf, var_bounds)\n', (34686, 34706), True, 'import arch.univariate.recursions_python as recpy\n'), ((2371, 2407), 'timeit.Timer', 'timeit.Timer', (['code'], {'setup': 'self.setup'}), '(code, setup=self.setup)\n', (2383, 2407), False, 'import timeit\n'), ((2843, 2865), 'numpy.ones', 'np.ones', (['(cls.nobs, 2)'], {}), '((cls.nobs, 2))\n', (2850, 2865), True, 'import numpy as np\n'), ((10089, 10103), 'numpy.abs', 'np.abs', (['resids'], {}), '(resids)\n', (10095, 10103), True, 'import numpy as np\n'), ((10920, 10934), 'numpy.abs', 'np.abs', (['resids'], {}), '(resids)\n', (10926, 10934), True, 'import numpy as np\n'), ((20415, 20434), 'numpy.exp', 'np.exp', (['lnsigma2[t]'], {}), '(lnsigma2[t])\n', (20421, 20434), True, 'import numpy as np\n'), ((22185, 22199), 'scipy.special.gamma', 'gamma', (['(j + 0.6)'], {}), '(j + 0.6)\n', (22190, 22199), False, 'from scipy.special import gamma\n'), ((23234, 23248), 'scipy.special.gamma', 'gamma', (['(j + 0.6)'], {}), '(j + 0.6)\n', (23239, 23248), False, 'from scipy.special import gamma\n'), ((31963, 31982), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (31969, 31982), True, 'import numpy as np\n'), ((32342, 32361), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (32348, 32361), True, 'import numpy as np\n'), ((32719, 32738), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (32725, 32738), True, 'import numpy as np\n'), ((33299, 33318), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (33305, 33318), True, 'import numpy as np\n'), ((33522, 33541), 'numpy.isfinite', 'np.isfinite', (['sigma2'], {}), '(sigma2)\n', (33533, 33541), True, 'import numpy as np\n'), ((33697, 33716), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (33703, 33716), True, 'import numpy as np\n'), ((33920, 33939), 'numpy.isfinite', 'np.isfinite', (['sigma2'], {}), '(sigma2)\n', (33931, 33939), True, 'import numpy as np\n'), ((34103, 34122), 'numpy.abs', 'np.abs', (['self.resids'], {}), '(self.resids)\n', (34109, 34122), True, 'import numpy as np\n'), ((34326, 34345), 'numpy.isfinite', 'np.isfinite', (['sigma2'], {}), '(sigma2)\n', (34337, 34345), True, 'import numpy as np\n'), ((34611, 34630), 'numpy.log', 'np.log', (['(20.0 / 10.0)'], {}), '(20.0 / 10.0)\n', (34617, 34630), True, 'import numpy as np\n'), ((11204, 11304), 'numpy.array', 'np.array', (['[1.0, resids[t - 1] ** 2.0, resids[t - 1] ** 2.0 * (resids[t - 1] < 0),\n    sigma2[t - 1]]'], {}), '([1.0, resids[t - 1] ** 2.0, resids[t - 1] ** 2.0 * (resids[t - 1] <\n    0), sigma2[t - 1]])\n', (11212, 11304), True, 'import numpy as np\n'), ((22203, 22215), 'scipy.special.gamma', 'gamma', (['(j + 1)'], {}), '(j + 1)\n', (22208, 22215), False, 'from scipy.special import gamma\n'), ((22218, 22228), 'scipy.special.gamma', 'gamma', (['(0.6)'], {}), '(0.6)\n', (22223, 22228), False, 'from scipy.special import gamma\n'), ((23252, 23264), 'scipy.special.gamma', 'gamma', (['(j + 1)'], {}), '(j + 1)\n', (23257, 23264), False, 'from scipy.special import gamma\n'), ((23267, 23277), 'scipy.special.gamma', 'gamma', (['(0.6)'], {}), '(0.6)\n', (23272, 23277), False, 'from scipy.special import gamma\n'), ((11094, 11145), 'numpy.array', 'np.array', (['[1.0, backcast, 0.5 * backcast, backcast]'], {}), '([1.0, backcast, 0.5 * backcast, backcast])\n', (11102, 11145), True, 'import numpy as np\n'), ((20170, 20192), 'numpy.sqrt', 'np.sqrt', (['sigma2[t - 1]'], {}), '(sigma2[t - 1])\n', (20177, 20192), True, 'import numpy as np\n'), ((20241, 20257), 'numpy.abs', 'np.abs', (['stdresid'], {}), '(stdresid)\n', (20247, 20257), True, 'import numpy as np\n')]