Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion arch/tests/univariate/test_positions_and_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
in Python.
"""

import matplotlib.pyplot as plt
from arch.compat.matplotlib import HAS_MATPLOTLIB

import numpy as np
from numpy.testing import assert_allclose
import pandas as pd
Expand All @@ -29,6 +30,9 @@
)
from arch.utility.array import cutoff_to_index, find_index

if HAS_MATPLOTLIB:
import matplotlib.pyplot as plt

NOBS = 300
DATES = pd.date_range("2000-01-03", periods=NOBS, freq="B")
PARAMS = np.array([0.05, 0.05, 0.1, 0.8])
Expand Down Expand Up @@ -183,6 +187,7 @@ def test_forecast_start_below_minus_nobs(result, model):
model.forecast(result.params, horizon=2, start=start)


@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_hedgehog_plot_negative_start(result):
kwargs = {"horizon": 3, "step": 50}
fig_neg = result.hedgehog_plot(start=-100, **kwargs)
Expand Down
10 changes: 9 additions & 1 deletion arch/tests/univariate/test_string_options.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""String options of the univariate models are not case sensitive"""

from arch.compat.matplotlib import HAS_MATPLOTLIB

from functools import partial

import matplotlib.pyplot as plt
import numpy as np
from numpy.testing import assert_allclose
import pandas as pd
Expand Down Expand Up @@ -30,6 +31,9 @@
)
from arch.univariate.base import ARCHModelForecast, _align_forecast

if HAS_MATPLOTLIB:
import matplotlib.pyplot as plt

# A string that is not an option is a ValueError and anything else a TypeError
BAD_VALUES = [
pytest.param("bogus", ValueError, id="bogus"),
Expand Down Expand Up @@ -242,6 +246,7 @@ def test_forecast_method_invalid(returns, result, bad, exc, use_result):


@pytest.mark.parametrize("annualize", ["d", "w", "m"])
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_plot_annualize_case(result, annualize):
scale = {"d": 252, "w": 52, "m": 12}[annualize]
expected = result.conditional_volatility * np.sqrt(scale)
Expand All @@ -258,13 +263,15 @@ def test_plot_annualize_case(result, annualize):
@pytest.mark.parametrize(
("bad", "exc"), [("bogus", ValueError), ("Q", ValueError), (1, TypeError)]
)
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_plot_annualize_invalid(result, bad, exc):
with pytest.raises(exc, match=r"annualize must be one of 'd', 'w' or 'm'; got "):
result.plot(annualize=bad)
plt.close("all")


@pytest.mark.parametrize("plot_type", ["volatility", "mean"])
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_hedgehog_plot_type_case(result, plot_type, returns):
try:
for spelling in (plot_type, plot_type.upper(), plot_type.title()):
Expand All @@ -281,6 +288,7 @@ def test_hedgehog_plot_type_case(result, plot_type, returns):


@pytest.mark.parametrize(("bad", "exc"), BAD_VALUES)
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_hedgehog_plot_invalid(result, bad, exc):
with pytest.raises(exc, match=r"plot_type must be one of 'volatility' or"):
result.hedgehog_plot(plot_type=bad)
Expand Down
12 changes: 10 additions & 2 deletions arch/tests/univariate/test_validation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
functions in ``arch.univariate.base`` and of ``arch.univariate.diagnostics``.
"""

from arch.compat.matplotlib import HAS_MATPLOTLIB

from collections.abc import Callable
from fractions import Fraction
from types import MappingProxyType
from typing import Any

import matplotlib.pyplot as plt
import numpy as np
from numpy.testing import assert_allclose
import pandas as pd
Expand Down Expand Up @@ -43,6 +44,9 @@
from arch.univariate.diagnostics import excess_kurtosis, hill_estimator, var_ratio
from arch.utility.exceptions import StartingValueWarning

if HAS_MATPLOTLIB:
import matplotlib.pyplot as plt

NOBS = 250
INDEX = pd.date_range("2000-01-03", periods=NOBS, freq="B")
PARAMS = np.array([0.05, 0.05, 0.1, 0.8])
Expand Down Expand Up @@ -414,7 +418,8 @@ def call(**kwargs: Any) -> Any:
try:
return getattr(fitted(), name)(**kwargs)
finally:
plt.close("all")
if HAS_MATPLOTLIB:
plt.close("all")

return call

Expand Down Expand Up @@ -950,6 +955,8 @@ def constraints(**kwargs: Any):
),
)
def test_validation(row):
if not HAS_MATPLOTLIB and row.target in ("plot", "hedgehog"):
pytest.skip("matplotlib not installed")
run_row(row)


Expand Down Expand Up @@ -1002,6 +1009,7 @@ def test_forecast_of_fixed_result_checks_arguments():
assert fixed.forecast(horizon=np.int64(2)).mean.shape[1] == 2


@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_plot_error_does_not_leave_a_figure():
plt.close("all")
with pytest.raises(ValueError, match=r"^annualize must be"):
Expand Down
Loading