if not isinstance(out, np.ndarray):
out = np.asarray(out)
return out
-
-
-def _lazywhere(cond, arrays, f, fillvalue=None, f2=None):
- """
- np.where(cond, x, fillvalue) always evaluates x even where cond is False.
- This one only evaluates f(arr1[cond], arr2[cond], ...).
- For example,
- >>> a, b = np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])
- >>> def f(a, b):
- return a*b
- >>> _lazywhere(a > 2, (a, b), f, np.nan)
- array([ nan, nan, 21., 32.])
- Notice it assumes that all `arrays` are of the same shape, or can be
- broadcasted together.
- """
- if fillvalue is None:
- if f2 is None:
- raise ValueError("One of (fillvalue, f2) must be given.")
- else:
- fillvalue = np.nan
- else:
- if f2 is not None:
- raise ValueError("Only one of (fillvalue, f2) can be given.")
-
- arrays = np.broadcast_arrays(*arrays)
- temp = tuple(np.extract(cond, arr) for arr in arrays)
- tcode = np.mintypecode([a.dtype.char for a in arrays])
- out = _valarray(np.shape(arrays[0]), value=fillvalue, typecode=tcode)
- np.place(out, cond, f(*temp))
- if f2 is not None:
- temp = tuple(np.extract(~cond, arr) for arr in arrays)
- np.place(out, ~cond, f2(*temp))
-
- return out
import numpy as np
from scipy.stats import rv_discrete, nbinom, poisson
from scipy.special import gammaln
-from statsmodels.compat.scipy import _lazywhere
+from scipy._lib._util import _lazywhere
class genpoisson_p_gen(rv_discrete):
insurance claims data: dispersion modelling. ASTIN Bulletin 32: 143–157
"""
import numpy as np
-from scipy._lib._util import _lazywhere as lazywhere
+from scipy._lib._util import _lazywhere
from scipy.special import gammaln
def series_density(y, mu, p, phi):
- density = lazywhere(np.array(y) > 0,
- (y, mu, p, phi),
- f=density_otherwise,
- f2=density_at_zero)
+ density = _lazywhere(np.array(y) > 0,
+ (y, mu, p, phi),
+ f=density_otherwise,
+ f2=density_at_zero)
return density
from io import BytesIO
from itertools import product
+import warnings
import numpy as np
import pandas as pd
res.plot_recursive_coefficient(variables="x4")
fig = plt.Figure()
- with pytest.warns(Warning):
- # Just silence the warning
+ # Just silence the warning
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
out = res.plot_recursive_coefficient(fig=fig)
assert out is fig
res.plot_recursive_coefficient(alpha=None, figsize=(30, 7))
else:
iqr = mean
+ def _safe_jarque_bera(c):
+ a = np.asarray(c)
+ if a.shape[0] < 2:
+ return (np.nan,) * 4
+ return jarque_bera(a)
+
jb = df.apply(
- lambda x: list(jarque_bera(x.dropna())), result_type="expand"
+ lambda x: list(_safe_jarque_bera(x.dropna())), result_type="expand"
).T
nan_mean = mean.copy()
nan_mean.loc[nan_mean == 0] = np.nan
where n is the number of data points, S is the sample skewness, and K is
the sample kurtosis of the data.
"""
- resids = np.asarray(resids)
+ resids = np.atleast_1d(np.asarray(resids, dtype=float))
+ if resids.size < 2:
+ raise ValueError("resids must contain at least 2 elements")
# Calculate residual skewness and kurtosis
skew = stats.skew(resids, axis=axis)
kurtosis = 3 + stats.kurtosis(resids, axis=axis)
assert_almost_equal(pval, self.pval_asym_exp, 3)
def test_statistics_for_2d_input(self):
- stats = ddm.distance_statistics(self.x, self.y)
+ stats = ddm.distance_statistics(
+ np.asarray(self.x, dtype=float),
+ np.asarray(self.y, dtype=float)
+ )
assert_almost_equal(stats.test_statistic, self.test_stat_emp_exp, 0)
assert_almost_equal(stats.distance_correlation, self.dcor_exp, 4)
assert_almost_equal(stats.S, self.S_exp, 4)
def test_statistics_for_1d_input(self):
- x = np.array(range(1, 21))
+ x = np.array(range(1, 21), dtype=float)
y = x + np.log(x)
stats = ddm.distance_statistics(x, y)
except IGNORED_EXCEPTIONS:
pytest.skip('Failed with HTTPError or URLError, these are random')
- x = iris[:50]
- y = iris[50:100]
+ x = np.asarray(iris[:50], dtype=float)
+ y = np.asarray(iris[50:100], dtype=float)
stats = ddm.distance_statistics(x, y)
except IGNORED_EXCEPTIONS:
pytest.skip('Failed with HTTPError or URLError, these are random')
- x = quakes[:50]
- y = quakes[50:100]
+ x = np.asarray(quakes[:50], dtype=float)
+ y = np.asarray(quakes[50:100], dtype=float)
stats = ddm.distance_statistics(x, y)
(1, 0, 0, 0, np.array([0.5, 1.]), 'p'),
(1, 0, 0, 0, np.array([-0.2, 100.]), 'p'),
(2, 0, 0, 0, np.array([-0.2, 0.5, 100.]), 'p'),
- (20, 0, 0, 0, np.array([0] * 20 + [100.]), 'p'),
+ (20, 0, 0, 0, np.array([0.0] * 20 + [100.]), 'p'),
# ARI models
(0, 1, 0, 0, np.array([1.]), 'p'),
(0, 1, 1, 4, np.array([1.]), 'p'),
(1, 0, 0, 0, np.array([0.5, 1.]), 'q'),
(1, 0, 0, 0, np.array([-0.2, 100.]), 'q'),
(2, 0, 0, 0, np.array([-0.2, 0.5, 100.]), 'q'),
- (20, 0, 0, 0, np.array([0] * 20 + [100.]), 'q'),
+ (20, 0, 0, 0, np.array([0.0] * 20 + [100.]), 'q'),
# IMA models
(0, 1, 0, 0, np.array([1.]), 'q'),
(0, 1, 1, 4, np.array([1.]), 'q'),
# Check for invalid type and coerce to non-integer
try:
- params = np.array(params) * 1.0
+ params = np.array(params, dtype=object)
+ complex_types = (complex, np.complex)
+ is_complex = [isinstance(p, complex_types) for p in params.ravel()]
+ dtype = complex if any(is_complex) else float
+ params = np.array(params, dtype=dtype)
except TypeError:
raise ValueError('Parameters vector%s includes invalid values.'
% title)
mdata = mdata[['realgdp', 'realcons', 'realinv']]
data = mdata.values
data = np.diff(np.log(data), axis=0)
- A = np.asarray([[1, 0, 0], ['E', 1, 0], ['E', 'E', 1]])
- B = np.asarray([['E', 0, 0], [0, 'E', 0], [0, 0, 'E']])
+ A = np.asarray([[1, 0, 0], ['E', 1, 0], ['E', 'E', 1]], dtype="U")
+ B = np.asarray([['E', 0, 0], [0, 'E', 0], [0, 0, 'E']], dtype="U")
results = SVAR(data, svar_type='AB', A=A, B=B).fit(maxlags=3)
cls.res1 = results
#cls.res2 = results_svar.SVARdataResults()