Repository: statsmodels/statsmodels

Issue:
BUG: Implemented __getnewargs__() method for unpickling
Within the ARIMA class, **getnewargs**() method is implemented
in order to pass "endog" and "order" positional arguments to
the **new**() method when called upon.
Solves the error: "TypeError: **new**() missing 2 required positional
arguments: 'endog' and 'order'"



Discussion:

Resolving patch:
diff --git a/statsmodels/tsa/arima_model.py b/statsmodels/tsa/arima_model.py
--- a/statsmodels/tsa/arima_model.py
+++ b/statsmodels/tsa/arima_model.py
@@ -974,7 +974,6 @@ def fit(self, start_params=None, trend='c', method="css-mle",
 #so model methods are not the same on unfit models as fit ones
 #starting to think that order of model should be put in instantiation...
 class ARIMA(ARMA):
-
     __doc__ = tsbase._tsa_doc % {"model" : _arima_model,
                                  "params" : _arima_params, "extra_params" : "",
                                  "extra_sections" : _armax_notes %
@@ -990,6 +989,11 @@ def __new__(cls, endog, order, exog=None, dates=None, freq=None,
             mod.__init__(endog, order, exog, dates, freq, missing)
             return mod
 
+    def __getnewargs__(self):
+        return ((self.endog),
+                (self.k_lags, self.k_diff, self.k_ma),
+                self.exog, self.dates, self.freq, self.missing)
+
     def __init__(self, endog, order, exog=None, dates=None, freq=None,
                  missing='none'):
         p, d, q = order


Test patch:
diff --git a/statsmodels/tsa/tests/test_arima.py b/statsmodels/tsa/tests/test_arima.py
--- a/statsmodels/tsa/tests/test_arima.py
+++ b/statsmodels/tsa/tests/test_arima.py
@@ -1,4 +1,4 @@
-from statsmodels.compat.python import lrange, BytesIO
+from statsmodels.compat.python import lrange, BytesIO, cPickle
 
 import os
 import warnings
@@ -153,6 +153,7 @@ def test_summary(self):
         table = self.res1.summary()
 
 
+
 class CheckForecastMixin(object):
     decimal_forecast = DECIMAL_4
     def test_forecast(self):
@@ -2251,6 +2252,39 @@ def test_long_ar_start_params():
     res = model.fit(method='mle',start_ar_lags=10, disp=0)
     assert_raises(ValueError, model.fit, start_ar_lags=nobs+5, disp=0)
 
+
+def test_arma_pickle():
+    np.random.seed(9876565)
+    x = fa.ArmaFft([1, -0.5], [1., 0.4], 40).generate_sample(nsample=200,
+                                                             burnin=1000)
+    mod = ARMA(x, (1, 1))
+    pkl_mod = cPickle.loads(cPickle.dumps(mod))
+
+    res = mod.fit(trend="c", disp=-1)
+    pkl_res = pkl_mod.fit(trend="c", disp=-1)
+
+    assert_allclose(res.params, pkl_res.params)
+    assert_allclose(res.llf, pkl_res.llf)
+    assert_almost_equal(res.resid, pkl_res.resid)
+    assert_almost_equal(res.fittedvalues, pkl_res.fittedvalues)
+    assert_almost_equal(res.pvalues, pkl_res.pvalues)
+
+
+def test_arima_pickle():
+    endog = y_arma[:, 6]
+    mod = ARIMA(endog, (1, 0, 1))
+    pkl_mod = cPickle.loads(cPickle.dumps(mod))
+
+    res = mod.fit(trend="c", disp=-1)
+    pkl_res = pkl_mod.fit(trend="c", disp=-1)
+
+    assert_allclose(res.params, pkl_res.params)
+    assert_allclose(res.llf, pkl_res.llf)
+    assert_almost_equal(res.resid, pkl_res.resid)
+    assert_almost_equal(res.fittedvalues, pkl_res.fittedvalues)
+    assert_almost_equal(res.pvalues, pkl_res.pvalues)
+
+
 if __name__ == "__main__":
     import nose
     nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb'], exit=False)