Skip to content

PERF: Implement a minimal WLS for use in iterative estimation - #3048

Merged
josef-pkt merged 3 commits into
statsmodels:masterfrom
bashtage:minimal-wls
Feb 27, 2017
Merged

josef-pkt merged 3 commits into
statsmodels:masterfrom
bashtage:minimal-wls

Conversation

@bashtage

Copy link
Copy Markdown
Member

Implement minimal WLS that computes the bare minimum needed
for GLM and RLM model estimation.

closes #2287

@coveralls

coveralls commented Jun 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 85.77% when pulling 764e8de on bashtage:minimal-wls into d5ad1a6 on statsmodels:master.

@josef-pkt josef-pkt added this to the 0.9 milestone Jun 20, 2016
@josef-pkt

Copy link
Copy Markdown
Member

I'm adding 0.9, too late for 0.8

@josef-pkt

Copy link
Copy Markdown
Member

@bashtage Thanks, Do you know what the speed improvement is with this?

@bashtage

Copy link
Copy Markdown
Member Author

About a factor of 3 -- pinv is easily the fastest of pinv, qr and lstsq.

On Mon, Jun 20, 2016 at 10:53 PM Josef Perktold [email protected]
wrote:

@bashtage https://github.com/bashtage Thanks, Do you know what the
speed improvement is with this?

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#3048 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AFU5RZ1KIiMqqzyXUaOWPYNijJwuPxnRks5qNwvPgaJpZM4I6DXV
.

@josef-pkt

Copy link
Copy Markdown
Member

#2203 most likely we should move this to the special models in regression
(doesn't exist yet, but I have two PRs with two different special_model modules, for sparse OLS and for absorb)

"""
normalized_cov_params = None
if method == 'pinv':
pinv_wexog = np.linalg.pinv(self.wexog)

@thequackdaddy thequackdaddy Jun 21, 2016 •

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 way that we could pinv on self.exog only once? Seems like this will run every iteration and the result shoudl be same every time, if I understand correctly.

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.

On second thought, this is wexog, and the weight should change every iteration as mu updates. Never mind.

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.

Yes, the weight changes and so it isn't possible to improve that step.

One source of slowness is the sheer number of statistics that need to be
computed for the update dictionary - residuals, whitened residuals, scale.
For moderate datasets these impact speeds. For very large datasets, pinv
dominates.

On Tue, Jun 21, 2016 at 3:49 AM Peter Quackenbush [email protected]
wrote:

In statsmodels/regression/_tools.py
#3048 (comment)
:

  •          \* resid : Residuals using original data
    
  •          \* normalized_cov_params
    
  •          \* model : namedtuple with one field, weights
    
  •          \* scale : scale computed using weighted residuals
    
  •    Notes
    

  •    Does not perform and checks on the input data
    
  •    See Also
    

  •    statsmodels.regression.linear_model.WLS
    
  •    """
    
  •    normalized_cov_params = None
    
  •    if method == 'pinv':
    
  •        pinv_wexog = np.linalg.pinv(self.wexog)
    

On second thought, this is wexog, and the weight should change every
iteration as mu updates. Never mind.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/statsmodels/statsmodels/pull/3048/files/764e8deafd2470c1995a64f5cf417b4c9d8f8823#r67800816,
or mute the thread
https://github.com/notifications/unsubscribe/AFU5RbtA_ajB5eEWz-CPCBFiO3gnl__iks5qN1FHgaJpZM4I6DXV
.

@coveralls

coveralls commented Jun 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 85.757% when pulling b8621d7 on bashtage:minimal-wls into 402dcf6 on statsmodels:master.

@coveralls

coveralls commented Jun 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 85.757% when pulling 0003fae on bashtage:minimal-wls into 62d08d1 on statsmodels:master.

@coveralls

coveralls commented Jun 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 85.757% when pulling 930c70f on bashtage:minimal-wls into 62d08d1 on statsmodels:master.

@coveralls

coveralls commented Jun 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 85.777% when pulling 4c90241 on bashtage:minimal-wls into e6e94a1 on statsmodels:master.

@bashtage
bashtage force-pushed the minimal-wls branch 2 times, most recently from bf1126e to 27c549a Compare June 21, 2016 17:37
@bashtage

Copy link
Copy Markdown
Member Author

I tested with a very large dataset - 30,000 observation.

Using pinv took around 18s, lstsq took 12s and qr took 24. Using the full WLS was virtually identical to pinv, around 19s.

@bashtage

Copy link
Copy Markdown
Member Author

I think @kshedden made some improvements to remove obvious inefficiencies and so there isn't alot, other than using lstsq which is a bit faster.

The testing was with GLM using the notebook example using np.tile to repeat the data 100 times.

@bashtage

Copy link
Copy Markdown
Member Author

In a large RLM with 1000000 obs master takes 10s for fit and this lstsq takes 5s. Using lstsq is definitely helpful, but it isn't clear whether a dedicated class/function is better than adding lstsq as a fit option to WLS.

@josef-pkt

Copy link
Copy Markdown
Member

you switched to numpy lstsq which I think also uses svd, so any sizable differences would be surprising, or it uses a better setting for svd.

One possibility at the cost of loosing precision would be to use the normal equations directly which might be faster for large nobs but not large k_vars.

@thequackdaddy is working with a few million observations in GLM.

@josef-pkt

Copy link
Copy Markdown
Member

(I'm intermittently loosing internet now, fortunately after the rc release.)

are you doing the timing just for pinv/lstsq or including cov_params?

@bashtage

Copy link
Copy Markdown
Member Author

Numpy uses dgesv while I think SVD uses dgesdd in this case.

@bashtage

Copy link
Copy Markdown
Member Author

(I'm intermittently loosing internet now, fortunately after the rc release.)

are you doing the timing just for pinv/lstsq or including cov_params?

The times reported are just for the fit step. The parts that aren't covered by _MinimalWLS have a fixed cost across the two implementations.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.007%) to 89.268% when pulling 99aa97f on bashtage:minimal-wls into bb1db2b on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.034% when pulling ecaa2d8 on bashtage:minimal-wls into 42d2f57 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.079% when pulling a2aeb45 on bashtage:minimal-wls into 792a3c5 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.089% when pulling 0badccd on bashtage:minimal-wls into 47b7c9d on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.109% when pulling 4e39391 on bashtage:minimal-wls into 67dcd43 on statsmodels:master.

@coveralls

coveralls commented Feb 8, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 90.164% when pulling 7c5a861 on bashtage:minimal-wls into dcff8e6 on statsmodels:master.

Comment thread statsmodels/regression/_tools.py Outdated
from collections import namedtuple
import numpy as np

_MinimalWLSResults = namedtuple('_MinimalWLSResults',

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.

It is better to make this into simple Dummy/Holder/Bunch classes.
tuples are not forward compatible if we want to change what is returned.

Comment thread statsmodels/regression/_tools.py Outdated
else:
params, _, _, _ = np.linalg.lstsq(self.wexog, self.wendog)

model = _MinimalWLSModel(weights=self.weights)

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.

What's the purpose of adding this Model class?
Why not just use self, which is already a model class?

@josef-pkt

Copy link
Copy Markdown
Member

I think we should merge this soon.

Some future ideas:
I think we make this swappable. We can use the same pattern for WLS that have a different structure, e.g. ridge penalized or sparse matrices. (something similar for structured Ridge/L2 penalization is in the GAM/penalized spline PR (*). a draft version of SparseOLS is in #2711)
For this we will need to eventually fix a internal API for the MinimalWLSXxx classes and their use in IRLS.

This PR is fully backwards compatible (except for maybe some near singular corner cases), we don't have to settle on a new API yet.

(*) checking this: it uses data augmentation to implement the penalty, instead of solving the Theil/Ridge moment conditions directly
https://github.com/statsmodels/statsmodels/pull/2744/files#diff-c83444952c95fc7854675f85758d9006R329

@bashtage

bashtage commented Feb 9, 2017 via email

Copy link
Copy Markdown
Member Author

@josef-pkt

josef-pkt commented Feb 9, 2017 •

Copy link
Copy Markdown
Member

but it allows tuple unpacking and slicing/indexing, which breaks when we want to change the number of returns
a, b, c = results

(that's a problem that scipy, especially scipy.stats, has for making changes, and we have it also with most of the hypothesis tests)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) to 90.243% when pulling a9387b9 on bashtage:minimal-wls into c2acb1a on statsmodels:master.

Implement minimal WLS that computes the bare minimum needed
for GLM and RLM model estimation.

closes statsmodels#2287
Covariance isn't commonly needed and one extra fit of WLS can
be used to produce the normalized cov when required
Remove use of namedtuple in favor of Bunch
Provide a single, best practice definition of Bunch
Refactor all uses to use single definition of Bunhc
@coveralls

coveralls commented Feb 12, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.005%) to 90.303% when pulling b3f4a6e on bashtage:minimal-wls into e757e8b on statsmodels:master.

@josef-pkt josef-pkt left a comment

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.

I added a few comments, mainly spot checking the unit tests.

@josef-pkt

Copy link
Copy Markdown
Member

sorry last comment was for a different PR, I must have switched browser windows by accident.

@josef-pkt

Copy link
Copy Markdown
Member

in it goes

@bashtage Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PERF: speed up iteratively reweighted least squares

4 participants