Skip to content
Closed
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
47 changes: 38 additions & 9 deletions statsmodels/regression/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
__all__ = ['GLS', 'WLS', 'OLS', 'GLSAR']

import numpy as np
import pandas as pd
from scipy.linalg import toeplitz
from scipy import stats
from scipy import optimize

from statsmodels.compat.numpy import np_matrix_rank
from statsmodels.tools.data import _is_using_pandas
from statsmodels.tools.tools import add_constant, chain_dot, pinv_extended
from statsmodels.tools.decorators import (resettable_cache,
cache_readonly,
Expand Down Expand Up @@ -225,7 +227,8 @@ def fit(self, method="pinv", cov_type='nonrobust', cov_kwds=None,
else:
lfit = RegressionResults(self, beta,
normalized_cov_params=self.normalized_cov_params,
cov_type=cov_type, cov_kwds=cov_kwds, use_t=use_t)
cov_type=cov_type, cov_kwds=cov_kwds, use_t=use_t,
**kwargs)
return RegressionResultsWrapper(lfit)

def fit_regularized(self, method="coord_descent", maxiter=1000,
Expand Down Expand Up @@ -805,7 +808,7 @@ def __init__(self, endog, exog=None, rho=1, missing='none', **kwargs):
super(GLSAR, self).__init__(endog, exog, missing=missing,
**kwargs)

def iterative_fit(self, maxiter=3):
def iterative_fit(self, maxiter=3, rtol=1e-4):
"""
Perform an iterative two-stage procedure to estimate a GLS model.

Expand All @@ -816,21 +819,44 @@ def iterative_fit(self, maxiter=3):
----------
maxiter : integer, optional
the number of iterations
rtol : float, optional
Relative tolerance between estimated coefficients to stop the
estimation. Stops if

max(abs(last - current) / abs(last)) < rtol

"""
#TODO: update this after going through example.
for i in range(maxiter-1):
# TODO: update this after going through example.
history = {'params': []}
for i in range(maxiter - 1):
if hasattr(self, 'pinv_wexog'):
del self.pinv_wexog
self.initialize()
results = self.fit()
history['params'].append(results.params)
if i == 0:
last = results.params
else:
diff = np.max(np.abs(last - results.params) / np.abs(last))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not stop early if some params become very small, being close to zero.
I think adding also atol and using np.allclose with both would be better.

if diff < rtol:
break
last = results.params
self.rho, _ = yule_walker(results.resid,
order=self.order, df=None)
#why not another call to self.initialize
# why not another call to self.initialize
if hasattr(self, 'pinv_wexog'):
del self.pinv_wexog
self.initialize()
results = self.fit() #final estimate
return results # add missing return
if _is_using_pandas(history['params'][0], None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bashtage Was there a specific reason to converting the list, besides having a nicer print?
I'm planning on removing this for 0.7, because it make indexing difficult because of inconsistent behavior between pandas, numpy, and python lists.

A possible alternative would be to use a simple history subclass of python list that has a as_dataframe attribute, which could also add rho and maybe other things to the history DataFrame. But this would have to be introduced consistently for all histories in models with specialized iterative solvers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just making it more user friendly if it was every going ot be used by an end user.

history['params'] = pd.concat(history['params'], 1)
history['params'].columns = ['iter_' + int(i) for i in range(history['params'].shape[1])]
else:
history['params'] = np.column_stack(history['params'])
# Use kwarg to insert history

results = self.fit(history=history)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far we patch the results instance directly by assigning directly the attribute
results.history = history

That's up for review and being changed since just assigning attributes outside of __init__ is not really "nice", but having history as a fit keyword also feels strange. (I did both, using kwargs and direct assignment, last time I changed something similar.)


return results

def whiten(self, X):
"""
Expand Down Expand Up @@ -994,6 +1020,8 @@ class RegressionResults(base.LikelihoodModelResults):
adjusted squared residuals for heteroscedasticity robust standard
errors. Is only available after `HC#_se` or `cov_HC#` is called.
See HC#_se for more information.
history
Estimation history for iterative estimators
HC0_se
White's (1980) heteroskedasticity robust standard errors.
Defined as sqrt(diag(X.T X)^(-1)X.T diag(e_i^(2)) X(X.T X)^(-1)
Expand Down Expand Up @@ -1076,7 +1104,7 @@ class RegressionResults(base.LikelihoodModelResults):
_cache = {} # needs to be a class attribute for scale setter?

def __init__(self, model, params, normalized_cov_params=None, scale=1.,
cov_type='nonrobust', cov_kwds=None, use_t=None):
cov_type='nonrobust', cov_kwds=None, use_t=None, **kwargs):
super(RegressionResults, self).__init__(model, params,
normalized_cov_params,
scale)
Expand Down Expand Up @@ -1110,7 +1138,8 @@ def __init__(self, model, params, normalized_cov_params=None, scale=1.,
# TODO: warn or not?
self.get_robustcov_results(cov_type=cov_type, use_self=True,
use_t=use_t, **cov_kwds)

for key in kwargs:
setattr(self, key, kwargs[key])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a difference to just doing self.__dict__update(kwargs) which, IIRC, we use sometimes?


def __str__(self):
self.summary()
Expand Down